Help with for loop

Help with for loop
0

#1

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:


#2

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


#3

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.


#4

What are the pieces of a for loop?


#5

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?


#6

#7

let test=[];

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


#8

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


#9

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


#10

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


#11

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?


#12

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


#13

You betcha! Hang in there!


#14

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?


#15

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


#16

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


#17


What does Array.push() return?


#18

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


#19

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.


#20

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