Calculate the sum of an array by removing a string?

Viewed 85

I have an array with 3 indices for each student:

var students = [
    ['Toto', 4, 17],
    ['Titi', 11, 12],
    ['Tata', 12, 14]
];

I have to calculate the sum for each student. Except that, I must avoid the string.

My result obtained for the student Toto is 0 instead of 21.

I don't understand where the problem?

var students = [
  ['Toto', 4, 17],
  ['Titi', 11, 12],
  ['Tata', 12, 14]
];

var result = 0;

for (var i = 0; i < students.length; i++) {
  if (typeof(students[i]) === "number") {
    result += students[i];
  }
  console.log("Student " + students[i][0] + " has the result " + result);
}

6 Answers

You can simply use destructuring alongside Array.prototype.reduce here:

const students = [['Toto', 4, 17], ['Titi', 11, 12], ['Tata', 12, 14]];

for (const student of students) {
  const [ name, ...summands ] = student;
  console.log(`${name} scored ${summands.reduce((acc,val)=>acc+val,0)}`)
}

For an even more concise version, you can destructure right in the for-definition:

const students = [['Toto', 4, 17], ['Titi', 11, 12], ['Tata', 12, 14]];

for (const [ name, ...summands ] of students) {
  console.log(`${name} scored ${summands.reduce((acc,val)=>acc+val,0)}`)
}

This will also work for lists of summands varying in length:

const students = [['Toto'], ['Titi', 1,2,3,4,5,6], ['Tata', 12, 14]];

for (const [ name, ...summands ] of students) {
  console.log(`${name} scored ${summands.reduce((acc,val)=>acc+val,0)}`)
}

Inside your students array, every item is an array.

const students = [
  ['Toto', 4, 17],
  ['Titi', 11, 12],
  ['Tata', 12, 14]
];


for (let i = 0; i < students.length; i++) {
  const student = students[i];
  let result = 0;
  for (let j = 0; j < student.length; j++) {
    if (typeof(student[j]) === "number") {
      result += student[j];
  }
  
  }
  console.log("Student " + students[i][0] + " has the result " + result);
}

Your loop iterates over the students, not their grades: you need two loops.

Furthermore, your logging knows that each student is represented by an array whose first element is a string of the student name, so when you add the loop to iterate over the student grades, skip the first entry:

for (var i = 0; i < students.length; i++) {
  var result = 0;
  for (var j = 1; j < students[i].length; j++) {
    result += students[i][j];
  }
  console.log("Student " + students[i][0] + " has the result " + result);
  }
}

Note I've also moved your result to count per student, you were counting for all students.

There are other ways to do this, but that might not be relevant atm.

you can run this

var students = [
  ['Toto', 4, 17],
  ['Titi', 11, 12],
  ['Tata', 12, 14]
];



for (var i = 0; i < students.length; i++) {
  var result = 0;
  for (var j = 0; j <= students[i].length; j++) {
    if (typeof(students[i][j]) === "number") {
      result += students[i][j];
    }
  }

  console.log("Student " + students[i][0] + " has the result " + result);
}

 var students = [
            ['Toto', 4, 17],
            ['Titi', 11, 12],
            ['Tata', 12, 14]
        ];

        let result = 0;
        students.forEach(item => {
            item.forEach(realItem => {
                if (typeof realItem === 'number'){
                    result += realItem
                }
            })
        })
        console.log(result)

You can do this with for loop

var students = [
  ['Toto', 4, 17],
  ['Titi', 11, 12],
  ['Tata', 12, 14]
];

var result = 0;

for (var i = 0; i < students.length; i++) {
  console.log("Student " + students[i][0] + " has the result " + (students[i][1] + students[i][2]));
}

or this with for..of and Array.prototype.reduce()

var students = [
    ['Toto', 4, 17],
    ['Titi', 11, 12],
    ['Tata', 12, 14]
];

for(let [name, ...values] of students) {
 const sum = values.reduce((acc, curr) => acc += curr, 0);
 console.log("Student " + name + " has the result " + sum);
}

Related