-3

I'm new to programming and it's a Noob question, but I couldn't find any thread that really explain each part to the bottom. I know how to create for loops, but some basic parts aren't clear to me. For example, What makestotal += v[i];do the next action 10+20+30+40+50 =150 What I mean that if I declared total = 0; why each value doesn't erase the previous value? e.g: loop run, first value is 10, then 20 run over 10, then 30 run over 20... instead it's keeping the numbers and adding them 10+20...and calculate.

And why I don't need to write var total = 0 ?

var v = [10,20,30,40,50];

var items = v.length;
total = 0;
for (var i = 0; i <items; i++){
    total += v[i];
}
var mean = total / items;
alert(mean);
N00bDev
  • 67
  • 8
  • total += v[i]; is shorthand for total = total + v[i]; – proggrock Aug 18 '16 at 17:24
  • 2
    I would love to help you, but this is a rather loaded question with a very broad scope of answers, which, unfortunately is not within the guidelines of questions on Stack Overflow. Please refer to an intro to javascript guide/tutorial/course, they should go over all of this as well as many other things – mhodges Aug 18 '16 at 17:24
  • The `var` keyword is used for determining the scope of your defined variables. It's not a requirement, but in your example `total` is a global field. You can read about it [here](http://stackoverflow.com/a/3892715/4204026). – Drew Kennedy Aug 18 '16 at 17:29

4 Answers4

1

You write "var total = 0" at the start to initialize the variable "total". This is important because you want to use it outside of your for loop. If you put it inside your for loop, every time the loop restarted it would go back to 0.

Since the value exists outside of the scope of your loop, it will not reset each time the loop restarts. That's why your total increases every time you call "total += v[i];".

nutshell
  • 11
  • 1
0

Javascript, among many other languages defines the += operator along with others such as -=, *=, \= etc. All of these operators the perform an operation on the current value of a variable.

So: total += v[i]; means the same thing as total = total + v[i];

Similarly total /= items; is the same as total = total / items;

Zev Isert
  • 915
  • 11
  • 20
0

Loops are handy, if you want to run the same code over and over again, each time with a different value.

for (statement 1; statement 2; statement 3) {
    code block to be executed
}

Statement 1 is executed before the loop (the code block) starts.

Statement 2 defines the condition for running the loop (the code block).

Statement 3 is executed each time after the loop (the code block) has been executed. We need for loops because you don't want to make same thing 100 or 1000 times by yourself. You said we we need loop to add numbers when we can add 10+20... You can add 10+20+30+40... but you can't add 1 million numbers for example. Another think is that we use loops because we often don't know the values of array.

total += v[i]; is equal to total = total + v[i];

You write var total = 0 because the starting value of the sum is 0. Also you want to declare var total = 0 outside of for loop because if you insert inside of that loop each time that loop will repeat the value of total would be 0.

Edison Biba
  • 4,384
  • 3
  • 17
  • 33
0

total += v[i] is the same as total = total + v[i] so this is a simple way of doing:

var helper = total + v[i]
total = helper

take a look at this resource, it very useful.

Iván Rodríguez Torres
  • 4,293
  • 3
  • 31
  • 47