-3

this is basically the code, I already got the right answer but I'm just trying to figure out how the process work

total = 20

   var myArr = [ 2,3,4,5,6];
    var total = 0;

    for (var d = 0; d < myArr.length; d++){
       total += myArr[d];

    }

and I did this

   var myArr = [ 2,3,4,5,6];
var total = "";

for (var d = 0; d < myArr.length; d++){
   total += myArr;

}

the output is.... (total is =""; so I can see what's happening inside but..

total= 2,3,4,5,62,3,4,5,62,3,4,5,6

and got confuse then I change the myArr to d

var myArr = [ 2,3,4,5,6];
var total = 0;

for (var d = 0; d < myArr.length; d++){
   total += d;

}

why is it

total = 10?

  • strings aren't the same as numbers, and whole arrays aren't the same as individual items from arrays – Jaromanda X Dec 19 '17 at 03:44
  • 2
    When you declare the variable `total = "";` you're declaring it as a string, whereas when you say `total = 0;` you make it a number. Therefore using the `+=` operation, in the first case you are adding strings, in the second you're actually adding numbers to numbers (it's called maths ;) ) – Adriano Dec 19 '17 at 03:44

2 Answers2

0

I've added comments to your working solution as below.

//Create new array to store our range
var myArr = [ 2,3,4,5,6];
//create new variable to store our total at the end
var total = 0;

//repeat code within when d < the length (or number of values) in our array, increment d each time it runs
for (var d = 0; d < myArr.length; d++){
   //take the current total and add the current value in our array to it each time
   // first time it will be 0 (total) + 2 (myArr[0]) second time it will be 2 (total) + 3 (myArr[1]) and so on.
   total += myArr[d];

}

As another user has pointed out in your second attempt you've changed the datatype of total from a number to a string.

hylian
  • 550
  • 1
  • 4
  • 19
  • Wow I didn't see that, thank you everyone for the TIME and ATTENTION !! also in this code.. `total =0; total += [d];` why is the answer =10? it suppose to be like "answer " = 5? or 6 – Spaceboard Dec 19 '17 at 18:32
  • Are you asking why the value is 10? The reason is because you're adding the value of [d] which is your counter. The counter increases each time the loop is executed (note the d++), there are 5 items in your array so it would be looped 5 times, so it would look something like (0 + 1 + 2 + 3 + 4). – hylian Dec 20 '17 at 04:38
  • hylian thank you so much man!!! love it!! ( love it mean's your Time and Attention) also so the answer should be 5 or 6 not 10? I got learn so far is that when you declaring a for loop operation + .length the function excecute the same as the length which is about 4 or 5 item's so why is it 10? really sorry for asking this new beginner's question, just started learning JS. for Alexa Skills.. – Spaceboard Dec 21 '17 at 21:52
  • @Spaceboard it would be 5 if it was written as total = [d]; so your counter 'd' is updated each time the loop is run with the end result of d being set to 5 as the loop as run 5 times. Because you have it as total += [d] it's taking the current value of total and adding D to it each time, so it would start off as 0, then it runs 1 time so total is now 1, then it's run 2 times so total is now 3 because 1+2 = 3, and so on. Does that make sense? – hylian Dec 22 '17 at 03:12
  • hylian!! wow lol I didn't even know what the += thanks for the clarification, and hylian thanks for everything and I thing the small detail about += will help me solve this problem next " 5! = 1*2*3*4*5 = 120" `function factorialize(num) { var num1; for(num1 = 0; num1 <= num; num1++ * ++num1){ return num1; } } factorialize(5); ` this is my code for now but I think using *= would solve it Hylian thank's for everything and just amaze how the universe work!! you have a AWESOME of Christmas dude. – Spaceboard Dec 22 '17 at 05:33
0

In the first instance:

var myArr = [ 2,3,4,5,6];
var total = 0;

for (var d = 0; d < myArr.length; d++){
    total += myArr[d];
}

You're creating a new variable outside the scope of the loop which is just 0. As you loop through each iteration, you're adding the current index of the array (myArr[d]) to the total variable.

In your second one, you're creating a new variable that's an empty string. JavaScript doesn't know or care that you're trying to add numbers, so it appends the number to an empty string, hence your result.

var myArr = [ 2,3,4,5,6];
var total = "";

for (var d = 0; d < myArr.length; d++){
  total += myArr;
}

The += operator applies to strings as well as numbers, and behaves differently depending on what the values are.

0 + '1' - 2 + '-3' // "-1-3"
0 + '1' - 2 // -1
Phix
  • 9,364
  • 4
  • 35
  • 62