JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

Jiten Mehta
Jiten Mehta
Front End Web Development Techdegree Student 7,929 Points

Struggling with for loops code challenge

The challenge asks to log out every number from 4 to 156.

Thie works in workspaces, but for some reason, it does not work in the code challenge. I can't see where I am going wrong? Help, please!

Code is below.

Thanks, Jiten.

script.js
var html='';

for ( var i = 4; i <= 156; i +=1 ) {
html += '  ' + i + '  ';
}

   console.log(html);

2 Answers

Martin Jones
Martin Jones
Front End Web Development Techdegree Graduate 41,143 Points

Your code will display every number from 4 to 156, but as one big string.

The challenge is to log out each number individually. To do this, simply move the console log to within the for loop, as shown below:

var html='';

for ( var i = 4; i <= 156; i +=1 ) {
    console.log(i);
}