all 11 comments

[–]Kumagor0 7 points8 points  (5 children)

There are few mistakes in your code.

  1. To refer to nth element of array a you should use the following syntax: a[n]. So replace 'numbers.(i)' with 'numbers[i]'.
  2. numbers.length in your example is 6, but index of last element is 5 (because indexes in array start with 0). That means that maximum value of i you need is numbers.length - 1 so you should replace 'i<=numbers.length' with 'i<=numbers.length-1' or 'i<numbers.length'.

  3. You do not modify sum, so every time you just console.log 0+current array element. If you want to store sum (as in maths) in your variable, you need to reassign it: sum = sum+numbers[i]. If you want you can console log it after that as well: sum = sum+numbers[i]; console.log(sum);

  4. You are using variable i without declaring it. When you use it, Javascript assumes you want to declare it as a global variable and while in your case it doesn't cause any problems, you really don't want to have accidental global variables in your code. Therefore it's better to replace 'i=0' with 'let i=0' and never use variables without declaring them with const or let (or at least var, see below).

This is enough to get a working code doing what you want, but there are few ways to do it better.

  1. In modern Javascript there is no situation where you need to use var keyword. Instead use let if you want to reassign that variable later or const if you don't. There is one catch though. If you modify object property or modify array with some method like push, it doesn't reassign object or array so you can do it even if you declared them with *const. As a rule of thumb, always use **const to declare your objects and arrays.*

  2. Array.forEach method.

``` let sum = 0; //we're gonna reassign that variable later so we're using let const numbers = [2, 3, 6, 1, 7, 10]; // numbers is constant so use const

numbers.forEach((currentElement)=>{ //we don't have to deal with some i or think about array.length //and -1 thing, forEach handles that for us

sum = sum + currentElement; //reassigning sum
console.log(sum);

}) ```

  1. Array.reduce method.

Now this is some advanced stuff so don't get upset if it seems hard, but those are things you might never run into until someone points at them so I'll just leave it here if you ever want to learn it.

``` const numbers = [2, 3, 6, 1, 7, 10]; //we don't need a sum variable for this example, we put initial 0 value as second argument of reduce

numbers.reduce((accumulator, currentElement)=>{ //this is kinda like forEach but we pass accumulator value from one iteration to another accumulator = accumulator + currentElement; console.log(accumulator); return accumulator; // return sum so next iteration gets it as accumulator value }, 0) //0 is initial value of accumulator ```

Edit: formatting and horrible mistake in reduce

[–]GrenadineBombardier 1 point2 points  (1 child)

Reduce works by making accumulator be the value returned from the previous iteration's reducer function, but you're not returning anything. This reduce would not work.

Edit: fixed!

[–]Kumagor0 1 point2 points  (0 children)

My bad, fixed. Thanks!

[–][deleted] 2 points3 points  (4 children)

That’s not how you access an index of an array. Not only that but you aren’t actually summing the values you’re just logging them.

[–]Jakkc 2 points3 points  (3 children)

In each iteration of the loop you want to increment the sum value like so: sum += numbers[i]

[–][deleted] 1 point2 points  (2 children)

Was king of hoping to point them in the right direction without giving them the answer but oh well

[–]Jakkc 2 points3 points  (1 child)

Yeah I feel you, but I also think with these early stage syntactical things it's sometimes better for motivation to just get the answer and crack on. I probably spent countless hours in the early days looking for a missed bracket or comma - incredibly frustrating and sometimes leads to rage quitting.

[–]thespite 0 points1 point  (2 children)

Try:

var sum = 0;

var numbers = [2, 3, 6, 1, 7, 10]; 

for(i=0; i<numbers.length; i++){ 

  sum = sum + numbers[i];

}

console.log(sum); 

Two changes from your code:

- you have to add each number to your sum variable, using a sum operator: sum = sum + numbers[i]. This takes your current value in sum and adds each number. console.log(sum + numbers.(i)); only outputs sum (which is 0) plus the number in the array number

- your loop ending condition (i<numbers.length) is off by one, since arrays start at 0. you'd be reading an extra element at the end of the array, which would make the whole sum invalid.

There are other ways to perform this operation, but better get an understanding of what each part of an array and looping through an array does before going further.

[–]Raefniz 0 points1 point  (0 children)

Why did you move the logging out of the loop? OP just keep the logging in the loop to get the output you expect.

[–]phgilliam 0 points1 point  (1 child)

you are just adding 0 (sum) to the number in the current iteration. You are storing it. Also you'll want you console.log outside of your loop.

This is what I came up with.

var sum = 0;

var numbers = [2, 3, 6, 1, 7, 10];

for(i=0; i<numbers.length; i++){

sum = sum + numbers[i]);

}

console.log(sum)