function processData(input) { //Enter your code here input = input.split('\n') var count = input[0]; var journey = input[1].split(''); var tracker = 0; var valleys = 0; var prev; journey.forEach(function(item, i) { if (item === 'U') { tracker++; } else if (item === 'D') { tracker--; } if (tracker === 0 && i !== 0 && prev < 0) valleys++; prev = tracker; }); 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); });