Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
My solution in JavaScript for the kata kyu 5 City Swim - 2D passes the tests but not the attempts

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Got it by adding -1 to x on towers.splice(secondIndex+1,x)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!