O'Reilly Forums: Easy Javascript Question From Chapter 4 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Easy Javascript Question From Chapter 4

#1 User is offline   wheresdavid 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 20-February 13

Posted 20 February 2013 - 08:40 PM

Hello Everyone,

I needed help better understanding a problem in the book.


function addUp(numArray){
var total = 0;
for (var i = 0; i < numArray.length; i++){
total += numArray[i];
}//close for loop
return total;
}//close function addUp

var theTotal = addUp([1, 5, 3, 9]);

What is the theTotal = ____

Which I know is 18.

My question is what does += mean? Does it mean first add both the they equal the answer? A example would be Great Thank You.

Also I having trouble understanding the loop I understand the i++ will increase 0 to one up which is 1 and so forth but I dont understand how this would fit into (total += numArray.length[i];)

Please explain. Thank You

This post has been edited by wheresdavid: 20 February 2013 - 08:42 PM

0

#2 User is offline   wheresdavid 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 20-February 13

Posted 21 February 2013 - 07:20 PM

Thank You I think I got that part

So given:

var y = 9;
y+=10;
---------------
y = 9 + 10 = 19
y = 19;
---------------------------------


I do have 2 more questions on the for loop. I dont know how to explain it.

It starts off like this:
for (var i = 0; i < numArray.length;i++){
total += numArray[i];
}

so it would turn to this.

I wrote it in the picture below.
with the array being in the green box.

Im not getting this part of putting the array into the for loop and getting the initialization (i = 0) added to the (numArray[i]; )

Posted Image
0

#3 User is offline   wheresdavid 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 20-February 13

Posted 21 February 2013 - 09:00 PM

Thank You So much "lemuel"

I finally understood it.
This is my second run at trying to learn Javascript by myself. First time after work I'd study from 6 - 10 pm go home sleep and again next day Monday - Saturday for 3-4 months and finally got burned out. Im here again after resting for 2 months trying to learn this. Thank You

I got it below:

var total = 0;
total = total + numArray[i];


numArray[0] = 1
1 = total + 1;

numArray[1] = 5
6 = 1 + 5;

numArray[2] = 3
9 = 6 + 3;

numArray[3] = 9
18 = 9 + 9;

What is theTotal___? = 18

Thank You.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users