I'm trying to solve this kyu 5 kata: https://www.codewars.com/kata/58e77c88fd2d893a77000102/
My solution passes the tests but not the attemps, and I cant figure out why. This is my code:
function rainVolume(towers) {
let res = 0;
return (function f(){
if(towers.length>=3){
let greatestIndex = towers.indexOf(Math.max(...towers));
towers[greatestIndex] = -1;
let secondIndex = towers.indexOf(second = Math.max(...towers));
towers.splice(greatestIndex,1);
//towers.splice(secondIndex,1)
let between = (x=greatestIndex-secondIndex)<0 ? towers.splice(greatestIndex,x=Math.abs(x)-1) : towers.splice(secondIndex+1,x);
res += second*between.length-between.reduce((a, b) => a + b, 0);
f(towers);
}
return res
})();
}
Basicly I thought that if I took the positions of the largest tower and the second largest in the array (greatestIndex and secondIndex), then multiply the second largest tower's high by the number of towers that are between greatestIndex and secondIndex, I could get the rain volume between those positions by substracting the sum of highs of the towers that are in between.
My english is poor and I dont know If I explained it right
Got it by adding -1 to x on towers.splice(secondIndex+1,x)