Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda