Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Passing an Argument to a Function

Agnes Caringal
Agnes Caringal
6,239 Points

How can I make this challenge!...anyone can help me with function code..

what's wrong with my answer :(

script.js
function returnValue () {
  var value = Math.floor(Math.random()*6) +1;
  return returnValue
}
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

9 Answers

This code just passed for me:

function returnValue($value) {
  return $value;
}

To explain: Create a function:

function returnValue

That accepts one argument:

($value) 

And returns the value:

return $value;

The code challenges are very picky and require exactly what they ask for.

Agnes Caringal
Agnes Caringal
6,239 Points

yeah I passed also, thanks, i was confused with the second challenge, Im kinda confused with this one also :(

Code for passing second part:

function returnValue($value) {
  return $value;
}

var echo = returnValue('My argument');

It is important to learn how to quote code. Use three back ticks (`) located on the upper left of your keyboard followed by the language without a space in between. I usually have to double space between the text above and the `. Insert your code on the next line. Then on the line after your code, three more ` to close it.

Agnes Caringal
Agnes Caringal
6,239 Points

thank you so much, any tips on JS? i have a lot of things to learn :)

Looking at what you posted, my overall tip is to pay attention to the little details. They are everything in any type of programming. For the Treehouse challenges, take your time to read the instructions very carefully. If you don't you will not pass the challenges. The final tip is to take the time to deploy your own solutions implementing what you learned as you go along. It will stick in your mind better. Personally, I need to deploy some sites with some custom JavaScript to get it in my mind.

At the bottom of the answer is an option called Best Answer. Please click that because it signals that your issue was answered successfully.

hey thanks for this! I didn't manage to pass the second challenge without your help.

What jumps off the page to me is you have no semicolon after your return line. I will have to retake the challenge to see if anything else is wrong. Let me know if the semicolon solves it. If not, I will redo the challenge.

Agnes Caringal
Agnes Caringal
6,239 Points

actually this is the challenge question # 1 : Here's a simple challenge: create a function named returnValue() that accepts a single argument, then returns that argument. This isn't a useful function, but we want to make sure you know how to create a function that can accept information when it is called.

Agnes Caringal
Agnes Caringal
6,239 Points

I put the semicolon, but bummer shows like this.

Bummer! Make sure you return the argument that's passed into the function. In other words type return, a space, and the parameter name.

You need a variable in the ().

Agnes Caringal
Agnes Caringal
6,239 Points

function returnValue (upper) { var value = Math.floor(Math.random()*6) +1; return returnValue; }

Agnes Caringal
Agnes Caringal
6,239 Points

function returnValue (upper) { var value = Math.floor(Math.random()*6) +1; return returnValue; }

Agnes Caringal
Agnes Caringal
6,239 Points

function returnValue (upper) { var value = Math.floor(Math.random()*6) +1; return returnValue; }

Agnes Caringal
Agnes Caringal
6,239 Points

function returnValue (upper) { var value = Math.floor(Math.random()*6) +1; return returnValue; }

variables have to have a $ in front.

also, refer to the Markdown Cheatsheet for formatting quotes.

Did you add the semicolon?

If this is answered, please mark is so.

Agnes Caringal
Agnes Caringal
6,239 Points

so meaning when they said one argument, always inside the () of a function named?

For a function to accept an argument, there must be a variable in the (). One variable for each argument that needs to be passed into the function. Then you need to call the function. Did you pass that part of the challenge?

Agnes Caringal
Agnes Caringal
6,239 Points

Now that you've created the returnValue function, call it, by passing it a literal string value -- a series of characters in quote marks like this: 'My argument'. Store the results of the function in a variable named echo.

answer :

function returnValue($value) { var echo ('My argument'); return $value; }

bummer : Oops! It looks like Task 1 is no longer passing.