counting valleys javascript code example

Example 1: Counting Valleys

# HackerRank.com "Counting Valleys" problem
def countingValleys(n, s):
    total_valley = 0
    location = 0

    for i in range(n):

        if s[i] == 'D':
            if location == 0:
                total_valley = total_valley + 1
            location = location - 1
			continue
        elif s[i] == 'U':
            location = location + 1

    return total_valley

Example 2: Javascript counting valleys

function countingValleys(n, s) {     
  const min = 2;     
  const max = 1000000;     
  let isInValley = false;     
  let valleys = 0;     
  s = (typeof s === "string") ? s.split('') : s; 
  
  if (s.length >= min          
      && s.length <= max          
      && n === parseInt(n, 0)          
      && n >= min          
      && n <= max           
      && n === s.length) {                    
    
    s.map(steps => ((steps === "U") ? 1 : -1))               
      .reduce((prev, next) => {                    
      if (prev < 0 && !isInValley) {                         
        isInValley = true;                    
      }                    
      if ((prev + next) === 0 && isInValley) {                         
        valleys++;                         
        isInValley = false;                    
      }            
      
      return prev + next;                   
    });     
  }    
  
  return valleys; 
}