In node.js file array.map not accessing index in callback function. why not?

In this node.js file I am working on a coding solution where i manipulate every third or fifth element in the array without using loops. So using the map method and using index as one of the paramenters should let me achieve this. If i use this code in a regular javascript template on replit it works fine. Also the code works fine on codewars and vscode but it doesnt work in my node.js collection for whatever reason it wont access the index in the custom map functions. Why is that? Did i do something wrong or is it just a weird replit thing?

Repl link:
https://replit.com/@spudsnbuds/CW#Code-Wars/Operations%20with%20sequence.js

var calc = function(a) {
  let square = a.map(x => {
    if (x > 0){
      return x * x
    }else {
      return x
    }
  })
  let copy = [...square]
  
  let third = copy.map((x,i) =>{
    if ((i + 1) % 3 == 0){
      return x * 3
    }else {
      return x
    }
  }
  )

  let fifth = third.map((x,i) => {
    if ((i + 1) % 5 == 0){
      return x * -1
    }else {
      return x
    }
  })
  return fifth.reduce((curr,acc) => {
    return curr + acc
  })
}

console.log(calc([ 0, 2, 1, -6, -3, 3 ]))

The answer should be 31. Not 5 like when I run this code in my file

The code seems okay. Don’t know why is not working.

Did you tried to clear node_modules and package-lock.json files? Maybe they are corrupted or something. Try a new clean install of the dependencies.

2 Likes