countingValleys

    0

    0

    The countingValleys function counts the number of valleys on a given path. It splits the path into an array of strings and then loops through the array, checking to see if the current item is a "U" character. If so, it increases the count by 1; otherwise, it decreases the count by 1. Finally, it compares the current count to the number of valleys desired and returns the corresponding value.

    function countingValleys(steps,path){
      let arr=path.split("");
      let count=0;
      let count1=0;
      arr.forEach(item => {
        if(item === 'U'){
          count++;
        }else{
          count--;
        }
    
        if(count === 0 && item==='U'){
          count1++;
        }
      })
      return(count1)
    }
    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.