JavaScript/6kyu/Equal_sides_of_an_array.js
// If you are given an array with multiple answers, return the lowest correct index.
If you are given an array with multiple answers, return the lowest correct index.
If you are given an array with multiple answers return the lowest correct index .
function findEvenIndex(arr){
  if (arr.length === 1) { return 0 }
  let last = arr.length - 1;
  if (arr.slice(1).reduce((a,b)=>a+b) === 0){ return 0 }
  if (arr.slice(0, last).reduce((a,b)=>a+b) === 0){ return last }
  for (let i = 1; i < last; i++) {
    let before = arr.slice(0,i).reduce((a,b)=>a+b);
    let after = arr.slice(i + 1).reduce((a,b)=>a+b);
    if (before === after){ return i  }
  }
  return -1;
}
function findEvenIndex ( arr ) { if ( arr . length === 1 ) { return 0 } let last = arr . length - 1 ; if ( arr . slice ( 1 ) . reduce ( ( a , b ) => a + b ) === 0 ) { return 0 } if ( arr . slice ( 0 , last ) . reduce ( ( a , b ) => a + b ) === 0 ) { return last } for ( let i = 1 ; i < last ; i ++ ) { let before = arr . slice ( 0 , i ) . reduce ( ( a , b ) => a + b ) ; let after = arr . slice ( i + 1 ) . reduce ( ( a , b ) => a + b ) ; if ( before === after ) { return i } } return - 1 ; }
If you are given an array with multiple answers, return the lowest correct index.
If you are given an array with multiple answers return the lowest correct index .