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 trialShailesh Jain
1,333 PointsTask 2 of 2 function returnValue(age) { var echo = returnValue(age); return echo + " " +age; }
What the answer for this one
function returnValue(age) { var echo = returnValue(age); return echo + " " +age;
}
function returnValue(age) {
var echo = returnValue(age);
return echo + " " +age;
}
<!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>
3 Answers
Jennifer Nordell
Treehouse TeacherHi there, Shailesh! Your original question states that you are looking for a solution to this challenge. And while we could give you the exact code needed to pass this, I feel like it would be a better learning opportunity for you overall, if you understand where you're going wrong and try and rethink how you're coding this.
First, and foremost, there should be no changes to the HTML file. All of your code will happen strictly inside the script.js
file provided to you. Secondly, the function that you had at the end of Step 1 should not be altered. It seems that the confusion comes with calling the function. And this is fairly straightforward to explain. Let me show you an example:
function sayHi(name) { //begin function
return "Hi there, " + name; //return something from function
} //end function
console.log(sayHi("Shailesh")) // call the function. Send in "Shailesh" to be assigned to the name and return the greeting. Log this to the console.
If you were to run this in your browser console, you would see that it prints out "Hi there, Shailesh". We first say what the function is going to do when we tell it to run/execute. Then we actually have to give the order for it to run/execute. We do this outside the function by giving the name and then any data we need to send in inside a pair of parentheses. In this example the name of the function is sayHi
and the data I'm sending in is the string "Shailesh".
I think maybe you can get the answer with these tips, but please let me know if you're still stuck!
Steven Parker
231,236 PointsThe returnValue function should not be modified after task 1.
All of the task 2 code is supposed to go after the function, not inside it.
markmneimneh
14,132 Points@Steven Parker ... do you understand the concept of demo'ing a point? I am not trying to answer the challenge, nor I care about the challenge.
The key concept I am conveying the the person who asked the question is that ...... if you want to get the function to do something, you have to call it first; once you call it, you will find out what the function does or does not do. You really can learn a lot by practicing on the side, away from the challenge.
Please don't lecture again; I don't need your lectures.
Thank you.
Steven Parker
231,236 PointsMy comment was primarily for the benefit of Shailesh and any other students who might be confused by your answer. I understand that your "answer" was deliberately unrelated to the course challenge, but that might not have been clear to Shailesh or other students reading it.
So to you I make no lecture, but perhaps an appeal that you might consider the needs of those asking the question before you choose to post something to demo a point that is not intended to actually address their question.
markmneimneh
14,132 Pointsin it current form ... it returns nothing. You need to call the function first, after loading the script. Once you do that, you shuold see returnValue(10) in the browser
<!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">
returnValue(10);
</script>
returnValue(10)
</body>
</html>
Steven Parker
231,236 PointsA word of warning to Shailesh and other students reading Mark's answer — Mark's stated intention here is "demo'ing a point". Further, he has said, "I am not trying to answer the challenge, nor I care about the challenge."
Just bear that in mind while reading and don't get confused. One hint: there are definitely no changes to be made to the HTML file in this challenge!
Jennifer Nordell
Treehouse TeacherOn a side note: the original function written by the original poster does, in fact, return something. But because it's never called from outside itself this might not be apparent at first. If you run the original code and include a call from outside the function and pass in an argument you'll see that what you get is InternalError: too much recursion
and this is because what is being returned is a reference to the function returning it. But this topic is way off the scope of this particular course/challenge.
Also, the reason the person would see returnValue(10)
in their browser is not because you've included it in the script, but rather because you've included it in the actual HTML.