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 trialAmir Abaza
12,713 PointsNeed help
Stuck on this JavaScript task...
var temperatures = [100,90,99,80,70,65,30,10];
function print(message) {
document.write(message);
}
function printList(list) {
var listTemp;
for(var i=0; i<list.length; i+=1)
listTemp += list[i] + ' ';
}
printList(temperatures);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Amir,
You have a few problems here. The first issue is that you have written a print
function to write things to the document (i.e., into the body of the webpage). However, the challenge instructions don't ask you to write anything into the webpage, they ask you to log to the console:
Inside the loop, log the current array value to the console.
That said, your function doesn't actually send the temperature values anywhere, since your printList
function gets the temperatures from the array and adds them to a listTemp
variable but never actually returns the listTemp
variable (so your print
function ends up printing nothing).
If you did return your listTemp
variable at the end of the function, you would notice that it doesn't correctly represent the data you want to return. You have a leading undefined
and a trailing space, neither of which you are asked to log to the console (The undefined
comes from using the +=
operator with a variable that has been declared but has not been assigned a value; the trailing space comes from adding ' '
to every number in the array, including the last one).
As it turns out, these are both easy to fix, since you don't need to build up a listTemp
variable at all. Going back to the challenge instructions we see:
Inside the loop, log the current array value to the console.
That is, every time you are in the body of the loop, just log the current value.
Hope that clears things up.
Cheers,
Alex