2

This is the official hw question.

Write a function named sum_even_index whose parameter is a list/array of decimal numbers. Your function must return the sum of entries at the parameter's even indicies (e.g., the sum of the entries stored at index 0, index 2, index 4, and so on).

Ive been looking for hours and still cant figure it out. On this site most of the answers refer to the number in the array not the actual indice. This prints 0. But should print 4

function sum_even_index(x) {
  let sum = 0
  for (let i in x) {
    if (x.indexOf[i] % 2 === 0) {
      sum = sum + x[i];
    }

  }
  return sum;
}

console.log(sum_even_index([1, 2, 3, 4]))
Andy
  • 61,948
  • 13
  • 68
  • 95
Manuel
  • 37
  • 4

3 Answers3

1

If i understood you right you have to change this line if (x.indexOf[i] % 2 === 0) { to if (i % 2 === 0) {.

Update

ForEach loop

function sum_even_index(x){
  let sum = 0;
  
  x.forEach( (i, e) => {
    if (i % 2 === 0) {
      sum = sum + e;
    }
  })
  return sum;
}
console.log(sum_even_index([1,2,3,4]))

your approach but better you take the forEach loop

function sum_even_index(x){
  let sum=0;
  
  for (let i in x){
    if (i % 2 === 0) {
    sum = sum + x[i];
    }
    
  }
  return sum;
}
console.log(sum_even_index([1,2,3,4]))
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • thank you!!! i think i am confusing with python because from my understanding i thought i would be assigned to the values 1 2 3 4 and not the actual indices – Manuel Oct 07 '22 at 20:21
  • [You shouldn't really be using `for...in` to iterate over arrays](https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea). – Andy Oct 07 '22 at 20:25
  • @MaikLowrey `for (let i of x)` gives you the elements of the array ("of", not "in"). Yeah, weird. Also, as a one liner: `return x.reduce((sum, el, i) => i%2 ? sum : sum+el, 0);` – danh Oct 07 '22 at 20:25
  • @danh and Andy. Thanks for the tips! I just wanted to point out point i % 2. And a forEach or for loop would also do it. I'll add it to my answer. Thank you colleagues! – Maik Lowrey Oct 07 '22 at 20:31
  • @MaikLowrey - I meant to direct my comment to the questioner (Emmanuel). Sorry for the error. – danh Oct 07 '22 at 20:33
  • @danh I think it's good that you pointed it out to me. I have already adjusted it. Merci and have a nice weekend – Maik Lowrey Oct 07 '22 at 20:37
1

Here is a version with .reduce():

const data=[1,2,3,4,5];

console.log(data.reduce((a,c,i)=>
 a+(i%2?0:c),0))
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • 1
    Or `i%2?0:c` if you want even less characters... – kelsny Oct 07 '22 at 20:58
  • Yes - point taken! – Carsten Massmann Oct 07 '22 at 21:00
  • 1
    Less characters is not, of itself, of any value and potentially undesirable. Most code is minimised before transmission anyway so reducing variable names purely for the sake of fewer characters just makes for less readable code that is harder to maintain. *Cf* `data.reduce((sum, value, index) => ...)`. This answer also doesn't provide an initial value for the accumulator *a*, so it will throw an error if it's passed an empty array. – RobG Oct 08 '22 at 08:07
  • Thanks for these finer points, @RobG ! I adjusted my answer. With respect to variable names I also agree with you. However, in this particular case things are so easy, that I will simply stick with my "standard" names: `a`ccumulator, `c`urrent and `i`ndex. – Carsten Massmann Oct 08 '22 at 08:14
0

for...in is for objects, with arrays use for...of Don't know if it's the best way to do it, but works.

function sum_even_index(x) {
  let sum = 0
  for (const [i,v] in x.entries()) {
    if (i % 2 === 0) {
      sum = sum + x[i];
    }

  }
  return sum;
}

console.log(sum_even_index([1, 2, 3, 4]))
v.k.
  • 2,826
  • 2
  • 20
  • 29
  • Arrays are objects. ;-) It used to be that *for..in* with arrays was unreliable as indexes may not be returned in numeric order and it iterates over inherited properties also. – RobG Oct 08 '22 at 07:50
  • tanks for the knowledge :) – v.k. Oct 08 '22 at 16:26