Help with for loop

Tell us what’s happening:
Silly newbie question, but I don´t understand why this doesn´t work!

Your code so far

let test=[];

for(let i; let i=0; i < 100; i++){
	test.push[i];
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:

(let i; let i=0;
Can you describe what is going on here?

Yeah, of course: I´m trying to solve the famous FizzBuzz problem, by creating a new array in which to push values to. My idea was to loop all numbers up to 100 (should be 101, but whatever for now), and then add the conditionals (“If i%3===0, etc”). But I can´t seem to get the initial for loop working, lol.

What are the pieces of a for loop?

I checked the syntax, but I can´t seem to find anything wrong with it. Isn´t it a variable that will iterate, the conditions in which to iterate and the conditions that will happen after an iteration?

let test=[];

for(let i; let i=0; i < 100; i++){
test=test.push[i];
};
still gets me an error.

I thinks what @ArielLeslie is trying to point you towards is something syntactically wrong with the for loop / show you something important.

Thank you for the reply. I fail to see what though.

I can relate. I once spent four hours debugging a product only to find I forgot a + concating two strings…

It’s okay…all of us start somewhere. Even veteran developers get stuck on simple issues. That’s why coding teams have analysts and testers review their code. :grinning:

The answer is in the first line of the link that @ArielLeslie shared. How many optional expressions?

1 Like

OH.
OH.
NOW I see it. Thanks a lot!

1 Like

You betcha! Hang in there!

1 Like

Sorry, another silly question:
if I try to run this, this won´t work:
var test=[];

for(var i=0; i < 100; i++){
test.push(i);
};

This, on the other hand, does:

var str = “”;

for (var i = 0; i < 9; i++) {
str = str + i;
}

I don´t get why. Is it because of something inherent to Arrays?

This will create an array containing the numbers from 0 to 99.

Thank you. But why can´t I do “test=test.push(i)”?


What does Array.push() return?

It returns the array´s length, if I´m not mistaken. But why can´t I reassign test to a push method?

You are successfully assigning the value returned by test.push(i) (which as you say is the length of the array) to test. That means that test is now a number instead of an array.

1 Like

I´m having way too “mind = blown” moments for one night. Thanks a lot!