function findEvenIndex(arr){ var elS,elF,arrF,arrS; for(var i = 0; i < arr.length; i++){ arrF = arr.slice(0, arr[i]); arrS = arr.slice(arr[i] + 1, arr.length); elF = 0; for (var j = 0; j < arrF.length; j++){ elF += arrF[j]; } elS = 0; for (var v = 0; v < arrS.length; v++){ elS += arrS[v]; } //console.log(elF); //console.log(elS); if(elF == elS){ return arr[i]; } } } console.log(findEvenIndex([1,2,3,4,3,2,1])); //3 console.log(findEvenIndex([1,100,50,-51,1,1])); //1 console.log(findEvenIndex([50,60,70,80,70,60,50])); //3 

  • and what does this code do? - Grundy
  • Maybe because elF not equal to elS ? Add a condition or - Yuri
  • obviously the condition elF == elS never satisfied and since there is no other return , the value of the undefined function call - Grundy
  • you need to find an element in the array where the sum before it and after the elements are equal - lesha310392
  • @Yuri if ((elF equals elS) or (elF is not equal elS)) {}? - Igor

1 answer 1

Comrade! Use Array.prototype.slice correctly!

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

  arrF = arr.slice(0, i); arrS = arr.slice(i + 1, arr.length); 
  • 2
    can I explain? - L. Vadim
  • @ L.Vadim in the commentary on the issue already dropped: developer.mozilla.org/en/docs/Web/JavaScript/Reference/… . Using arrF = arr.slice(0, arr[i]); You get an array with a zero element and a long one, which is stored in the i-th element of the arr array. You need to trim i elements, so you should use arrF = arr.slice(0, i); - Semushin Sergey