Answers for "counting valleys hackerrank solution javascript"

0

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; 
}
Posted by: Guest on January-03-2021
0

counting valleys hackerrank solution javascript

'use strict';
 
const processData = input => {
    const lines = input.split('\n');
    const steps = lines[1];
    let valleys = 0;
    let seaLevel = 0;
 
    for(let i = 0; i < steps.length; i++) {
        let c = steps.charAt(i);
        if(c == 'D') {
            seaLevel -= 1;
        } else {
            seaLevel += 1;
        }
        if(seaLevel == 0 && c == 'U') {
            valleys += 1;
        }
    }
    console.log(valleys);
 
};
 
process.stdin.resume();
process.stdin.setEncoding("ascii");
 
let _input = "";
process.stdin.on("data", input => _input += input);
process.stdin.on("end", () => processData(_input));
Posted by: Guest on August-31-2020

Code answers related to "counting valleys hackerrank solution javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language