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 |
1 answer
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); - 2can 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 usearrF = arr.slice(0, i);- Semushin Sergey
|
elFnot equal toelS? Add a condition or - YurielF == elSnever satisfied and since there is no otherreturn, the value of the undefined function call - Grundy