#variable stays 0

58 messages · Page 1 of 1 (latest)

tulip adder
#

Hey, why does totalBirds stay on 0 here in js?

  let totalBirds = 0;
  for(let i = 0; i >= birdsPerDay.length; i++) {
    totalBirds += birdsPerDay[i]
  }
  return totalBirds
}```
sour sage
#

Disclaimer: I'm a noob. Sorry if I misunderstood.
Shouldn't it be i<= birdsPerDay ?

tulip adder
#

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

sour sage
#

But that part is the condition evaluated for it to do the loop, not to exit it, right?

tulip adder
#
  // code that is executed repeatedly as long as the condition is true
}```
tulip adder
#

the starting point is 0

sour sage
tulip adder
#

if it is greater or equal to the length, then it will stop

tulip adder
tribal sundial
# tulip adder if it is greater or equal to the length, then it will stop

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

tulip adder
#

oh did I miss something

#

shit

#

then I apologize @sour sage Sadge

#

sorry for confusing you

sour sage
#

Lol, no need to at all ❤️

#

Happy it got solved 😊

tribal sundial
#
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?

tulip adder
#

so as long as the condition is true, it will run?

tribal sundial
#

Yes

tulip adder
#

my code returns NaN now

tulip adder
#
  let totalBirds = 0;
  for(let i = 0; i <= birdsPerDay.length; i++) {
    totalBirds += birdsPerDay[i] 
  }
  return totalBirds
}```
tribal sundial
#

No error litterally NaN?

tulip adder
tribal sundial
#

Ah yeah cause it's js...........................................

tulip adder
#

oh

#

how should I solve this?

#

whats the problem

tribal sundial
#

any other language would throw an Error here lmao

tulip adder
tribal sundial
#

Think about array indexing, and what the length Property actually returns

#

from what number to which number does your loop currently go?

tulip adder
#

oh

#

index - 1 right

#

it works

tribal sundial
#

Where do we start counting when we talk about arrays?

tulip adder
#

0

#

I got it

#

it works now

tribal sundial
#

I still want you to clearly understand

#

So what index is the last element in an array that has a length of 7?

tulip adder
#

6

tribal sundial
#

Good

tulip adder
#

as I said, index - 1

#

right?

tribal sundial
#

depends what you mean with index...

tulip adder
#

array indexes ig

tribal sundial
#

What's your code now?

tulip adder
#
  var totalBirds = 0;
  for(let i = 0; i <= birdsPerDay.length - 1; i++) {
    totalBirds += birdsPerDay[i]
  }
  return totalBirds
}```
tribal sundial
#

ok yeah this works
another thing you can consider is to simply not go until i <= birdsPerDay.length but rather just go to i < birdsPerDay.length

#

then you also prevent your array indexing to go over the amount of elements

tulip adder
#

oh thats true

#

thank you so much for your time and effort PepeHearts