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 trialGreta Regondi
6,166 PointsRe-write code using a loop.
Can you help me?
2 Answers
Justin Horner
Treehouse Guest TeacherHello Greta,
The code challenge is asking for you to remove the redundant calls to console.log and instead use a loop to accomplish the same result. You can start with a for loop like this.
for(var i = 2; i < 25; ++i) {
}
In this case we want only want to log the even numbers. For that, we can use the modulus operator to find out of the variable "i" holds a value that is even.
if(i % 2 == 0) {
console.log(i);
}
If you put these together, you'll have this.
for(var i = 2; i < 25; ++i) {
if(i % 2 == 0) {
console.log(i);
}
}
I hope this helps.
elk6
22,916 PointsHi Greta,
Try using a for loop to console.log the numbers:
for (num = 2; num < 25; num = num + 2) {
console.log(num);
}
This loop makes a new var called num with the value 2, it checks if the value is still under 25 and, if so, executes the loope and add 2 to the num var. Meanwhile it logs the value of num to the console. Since we gave it a +2 it will log every other second number.
Victor Warner
2,869 PointsVictor Warner
2,869 PointsI like this answer than you