-1

I have read other question regarding to for-of loop and came up with this code calculating average and summ of all elements in array, there seems to be something incorrect, help would be appreciated.:

var i;
elmt=[]
elmt[0] = 0;
elmt[1] = 1;
elmt[2] = 2;
elmt[3] = 3;
elmt[4] = 4;
elmt[5] = 7;
elmt[6] = 8;
elmt[7] = 9;
elmt[8] = 10;
elmt[9] = 11;

var sum = 0;
for (let sum of elmt) {
    sum += parseInt( elmt[i], 10 ); 
}
var avg = sum/elmt.length;

document.write( "The sum of all the elements is: " + sum + " The average is: " + avg );
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Loops</h2>

<p id="demo"></p>

</body>
</html>

If you guys know other easier way doing the same thing using 'for-of' loop feel free to share.

FZs
  • 16,581
  • 13
  • 41
  • 50
  • You're using `elmt[i]` while calculating `sum`. Also, you're using the same name `sum` as your total sum variable and in `for` loop iterator. `elmt[i]` is `NaN`. – user3132457 Jan 19 '19 at 14:40
  • Why is `elmt` defined without `var` (or `let`/`const`)? And that's a really complicated way of adding the elements to `elmt` -> `var elmt = [0, 1, 2, 3, 4, 7, 8, 9, 10, 11];` would do the same. – Andreas Jan 19 '19 at 14:41
  • 1
    @user3132457 `elmt[i]` is not `NaN`, it's `undefined` because `i` is undefined. Only `parseInt(elmt[i], 10)` will be `NaN` – Andreas Jan 19 '19 at 14:44
  • @Andreas wanna know why i got down voted, my question was clear, i showed some code ... –  Jan 19 '19 at 14:57
  • 1
    Not my DV, but I would guess that it is the missing research on how loops work and maybe that there are no hints of debugging (why is `i` or `elmt[i]` `undefined`?). – Andreas Jan 19 '19 at 15:02
  • @Andreas my mistake there. –  Jan 19 '19 at 15:06

3 Answers3

1

You're using two kinds of loops. You can either choose a normal for loop, or a for...of loop:

// For loop
for (i = 0; i < elms.length; i++) { 
  sum += elmt[i];
}

// For...of loop
for (let e of elmt) { 
  sum += e;
}

But, a cleaner way of doing this is by using reduce:

var sum = elms.reduce((a, c) => a + c, 0);
var avg = sum / elms.length;
Rengers
  • 14,911
  • 1
  • 36
  • 54
0

Try the following

var elmt = [0,1,2,3,4,7,8,9,10,11];

//Using reduce()

function totalReduce(arr) {
  if(!Array.isArray(arr)) return;
  return arr.reduce((a, v)=>a + v);
}

//Using for loop

function totalForLoop(arr) {
  if(!Array.isArray(arr)) return;
  let totalNumber = 0;
  for (let i=0,l=arr.length; i<l; i++) {
     totalNumber+=arr[i];
  }
  return totalNumber;
}
// Using while loop

function totalWhile(arr) {
  if(!Array.isArray(arr)) return;
  let totalNumber = 0, i=-1;
  while (++i < arr.length) {
     totalNumber+=arr[i];
  }
  return totalNumber;
}
// Using array forEach

function totalForEach(arr) {
  if(!Array.isArray(arr)) return;
  let sum=0;
  arr.forEach(each => {
    sum+=each;
  });
  return sum;
};


var sum = totalReduce(elmt);

console.log( "Sum Using reduce: " + sum + " Average: " + sum/elmt.length );

sum = totalForLoop(elmt);
console.log( "Sum Using totalForLoop: " + sum + " Average: " + sum/elmt.length );

sum = totalWhile(elmt);
console.log( "Sum Using totalWhile: " + sum + " Average: " + sum/elmt.length );

sum = totalForEach(elmt);
console.log( "Sum Using totalForEach: " + sum + " Average: " + sum/elmt.length );

The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.

Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16
0
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Loops</h2>

<p id="demo"></p>

<script>
var i;
elmt=[]
elmt[0] = 0;
elmt[1] = 1;
elmt[2] = 2;
elmt[3] = 3;
elmt[4] = 4;
elmt[5] = 7;
elmt[6] = 8;
elmt[7] = 9;
elmt[8] = 10;
elmt[9] = 11;

var sum = 0;
for (let x of elmt) {
    sum += x;
}
var avg = sum/elmt.length;

document.write( "The sum of all the elements is: " + sum + " The average is: " + avg );
</script>

</body>
</html>

The assumption is that all the elements are numbers, other way you need a type checking to ignore NaN entries.

Marcel Preda
  • 1,045
  • 4
  • 18