0
var numbers = [{grades:[100,100,100]}];
var result = 0;

for (var i=0;i<numbers.length;i++){
    for (var p in numbers.grades[i]);

    result+=p+":"+numbers[i][p];
    //console.log(p+":"+numbers[i][p])
    console.log(result);
}

this is what i have so far. I know how to get all the numbers in the .grades but i want them all added up in a very simple way.

5 Answers 5

3

The problem you have is numbers.length is only 1. You want

numbers[0].grades.length; // 3

This should sum the grades for you

for (var i=0, sum=0; i<numbers[0].grades.length; i++) {
  sum += numbers[0].grades[i];
}

sum; // 300

If you have access to Array.prototype.reduce, you could use this

var sum = numbers[0].grades.reduce(function(a, b) { return a +  b; }, 0);

sum; // 300

Note .reduce requires ECMAScript >= 5 and will not work in IE <= 8

Sign up to request clarification or add additional context in comments.

1 Comment

Great solution! In this scenario it is safe to assume numbers[0], but we should wrap the reduce in a numbers.forEach function to enable any future development.
1

You almost had it

var numbers = [{grades:[100,100,100]}];
var result = 0;

for (var i=0;i<numbers.length;i++){
    for (var g=0; g<numbers[i].grades.length; g++) {

    result = result+numbers[i].grades[g];
    console.log(result);
    }
}

As discussed, if it does support ES5, you can write it like this:

var numbers = [{grades:[100,100,100]}];
var result = 0;

numbers.forEach( function(val) {
    result = result + val.grades.reduce( function (previousValue, currentValue, index, array) {
        return previousValue + currentValue;
    })
});

just remember that you need to loop through the numbers array, instead of giving it a default value of '0' // numbers[0]grades etc

Comments

0

or you can try:

for(var j=0; j<numbers.length;j++)
{
  for (var i=0, sum=0; i<numbers[j].grades.length; i++) {
    sum += numbers[j].grades[i];
  }
}
console.log(sum);

Comments

0

A more functional/javascript approach would be to use array.reduce():

var sum = numbers[0].grades.reduce(function(previousValue, currentValue, index, array){
    return previousValue + currentValue;
});

console.log(sum);

Comments

0

The list you want to loop through is :

var list = numbers[0].grades;

Then, inside the loop :

result += list[i];

That's it :)


How to browse a javascript array :

var a = ['I', 'need', 'the', 'basics'];
console.log(a[0]); // "I"
console.log(a[3]); // "basics"
console.log(a[a.length - 1]); // "basics"

How to browse a javascript object :

var o = { a: 'I', b: 'need', c: 'the', d: 'basics' };
console.log(o.a); // "I"
console.log(o.d); // "basics"
console.log(o['d']); // "basics"

All together :

var mixed = [{ a: 'foo' }, { b: 'bar' }, { c: ['baz'] }];
console.log(mixed[0].a); // "foo"
console.log(mixed[1].b); // "bar"
console.log(mixed[2].c[0]); // "baz"

To add up all grades :

var grades, i, j;
var sum = 0;
var list = [
    { grades: [1, 2, 3] },
    { grades: [4, 5, 6] }
];

for (i = 0; i < list.length; i++) {
    grades = list[i].grades;
    for (j = 0; j < grades.length; j++) {
        sum += grades[j];
    }
}

console.log(sum); // 21

1 Comment

@user3079589 Great :D But you should rather express your enthusiasm by voting or by accepting your favorite answer : meta.stackexchange.com/questions/5234/…. Keep enjoying :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.