countingValleys.js

    0

    0

    The code in this function is responsible for counting the number of valleys in a path. First, it splits the path string into an array of strArr objects. The count variable stores the current total count, and the result variable keeps track of the current total sum. Next, the code loops through each strArr object in the array, counting the number of times the letter "d" appears as a character. If the current count equals 0, then the code removes the "d" from the strArr object, and updates the count value. If the current count equals 1, then the code adds 1 to the count value. Finally, the code returns the total count and sum to the caller.

    function countingValleys(steps, path) {
      // Write your code here
    
      let strArr = path.split('')
      let count = 0
      let result = 0
      for(let step=0; step<steps; step++){
        if(count == 0 && strArr[step].toLowerCase() == 'd'){
          count -= 1
          result += 1
        } else if(strArr[step].toLowerCase() == 'd'){
          count -= 1
        } else {
          count += 1
        }
      }
      return result
    }
    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.