function processData(input) { const actions = input.split('\n')[1].split(''); const SEA_LEVEL = 0; let isBelowSeaLevel = false; let valleys = 0; let currentLevel = SEA_LEVEL; actions.forEach(function(action) { if(action === 'D') { currentLevel--; } else { currentLevel++; } if(currentLevel < SEA_LEVEL && isBelowSeaLevel === false) { isBelowSeaLevel = true; valleys++; } else if(currentLevel >= SEA_LEVEL) { isBelowSeaLevel = false; } }); console.log(valleys); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });