//sample solution
function replaceMiddle(arr) {
if(arr.length%2 === 0){
var middleElementUpperIndex = Math.floor(arr.length/2);
var middleElementLowerIndex = middleElementUpperIndex - 1;
var middleElementLower = arr[middleElementLowerIndex];
var middleElementUpper = arr[middleElementUpperIndex];
var middleElementSum = middleElementLower + middleElementUpper;
arr.splice(middleElementLowerIndex,2,middleElementSum);
}
return arr;
}
No comments:
Post a Comment