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
ab199
Full Stack JavaScript Techdegree Student 20,137 PointsAre there bugs in the JavaScript code challenges?
var temperatures = [100,90,99,80,70,65,30,10];
for (i = 0; i <= temperatures.length; i++) {
var currentValue = temperatures.shift();
console.log(currentValue);
}
I'm taking the "code challenges" for the JavaScript Arrays, Objects, and Loops course.
The code calls to iterate through all the values beginning from the first in the item index, to the last.
I used the shift method to make this possible. What am I doing wrong here?
2 Answers
Chris Shaw
26,676 PointsHi Yan,
Generally whenever a challenge asks you to iterate over an array we don't need to use any methods but instead square brackets to retrieve each value within the array, assuming what the challenge is asking for you simply need to use the variable i in your for loop to retrieve each value.
var temperatures = [100,90,99,80,70,65,30,10];
for (var i = 0; i <= temperatures.length; i++) {
console.log(temperatures[i]);
}
One other thing to note is the variable i should have the keyword var before it as i wasn't defined yet, this is good a practice to maintain as it will result in better code overall.
Happy coding!
Richard Hope
25,237 PointsThink you may need to use 'i' within your for loop function eg var currentValue = temperatures[i];