climbingLeaderboard.js

    0

    0

    This code calculates the ascending order of the scores for a group of climbers, Alice. The code initializes an array called alice to an initial length of 0. It then calculates the length of the array, sl, as well as the number of elements in the array, scores. The code then loops through each element in the array, a, counting up from 0 and assigning the current score, score, to alice[a]. In addition, the code checks to see if the current score, score, is greater than the score stored in the variable, redundant, which was initially set to 0. If the current score is greater, then the code updates the redundant variable to 1 and then continues the loop. If the current score is not greater than the redundant variable, then the code removes the element, a, from the array, alice, and updates the scores array to reflect the changes made to alice.

    function climbingLeaderboard(scores, alice) {
      const l = alice.length
      const sl = scores.length + 1
      
      for (let a = 0; a < l; a++) {
        let score = alice[a]
        let redundant = 0
        for (let b = 0; b < sl; b++) {
          if (scores[b] === scores[b - 1]) {
            redundant++
            continue;
          }
          if (scores[b] > score === false) {
            scores.splice(b, 0, "a")
            let c = scores.indexOf("a")
            alice[a] = (c + 1) - redundant
            scores.splice(c, 1)
            break;
          }
        }
      }
      
      return alice
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.