#variable stays 0
58 messages · Page 1 of 1 (latest)
Disclaimer: I'm a noob. Sorry if I misunderstood.
Shouldn't it be i<= birdsPerDay ?
no, the for loop ends when i is greater than the birdsPerDay array length
i is 0 so when I start the loop its already smaller than the array length when I use i<= birdsPerDay
But that part is the condition evaluated for it to do the loop, not to exit it, right?
// code that is executed repeatedly as long as the condition is true
}```
i >= birdsPerDay.length means that the loop will end when i is greater or equal to the array length
the starting point is 0
But here you say the condition needs to be TRUE for the code to be executed. When i=0 the condition is false...
What you wrote is the condition to END the loop. But the for(...) asks for a condition to KEEP executing it, right?
Again, sorry if I misinterpreted it. I'll leave the pros to answer this.
no, as long as i is not > or = to the array length, it will continue
if it is greater or equal to the length, then it will stop
its okay I appreciate the help 
Yes that's what you expected, but elshone is right... right now your condition i >= birdsPerDay.length
is False when i is smaller than birdsPerDay.length and True when i is bigger than birdsPerDay.length
for(let i = 0; i >= birdsPerDay.length; i++)
Since you start at 0 here with i, it will at the beginning probably be smaller than the birdsPerDay.length so the loop will most certainly not be executed at all
oh did I miss something
shit
then I apologize @sour sage 
sorry for confusing you
birdsPerDay = [1, 3, 4, 1, 0, 2, 5] //length will be 7
//looking at the for loop
for(let i = 0; i >= birdsPerDay.length; i++)
//You initialize i as 0, then check if i >= 7 which is False, then you increase i
//since i is 0 and the condition is False, the loop will not run
you were meant to do either
switching i with birdsPerDay.length so it is birdsPerDay.length >= i
or do
i <= birdsPerDay.length
All clear now?
so as long as the condition is true, it will run?
Yes
my code returns NaN now
alright thank you so much
let totalBirds = 0;
for(let i = 0; i <= birdsPerDay.length; i++) {
totalBirds += birdsPerDay[i]
}
return totalBirds
}```
No error litterally NaN?
Ah yeah cause it's js...........................................
any other language would throw an Error here lmao

Think about array indexing, and what the length Property actually returns
from what number to which number does your loop currently go?
Where do we start counting when we talk about arrays?
I still want you to clearly understand
So what index is the last element in an array that has a length of 7?
6
Good
depends what you mean with index...
array indexes ig
What's your code now?
var totalBirds = 0;
for(let i = 0; i <= birdsPerDay.length - 1; i++) {
totalBirds += birdsPerDay[i]
}
return totalBirds
}```