Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function walking(input) {
- let target = 10000;
- let sum = 0;
- let index = 0;
- let command = input[index];
- while (command !== 'Going home') {
- let steps = Number(command);
- sum += steps;
- if (sum >= target) {
- console.log(`Goal reached! Good job!`);
- console.log(`${sum - target} steps over the goal!`);
- break;
- }
- index++;
- command = input[index];
- }
- if (command === "Going home") {
- let stepsToHome = Number(input[index + 1]);
- sum += stepsToHome;
- if (sum >= target) {
- console.log(`Goal reached! Good job!`);
- console.log(`${sum - target} steps over the goal!`);
- } else {
- console.log(`${target - sum} more steps to reach goal. `);
- }
- }
- }
- walking(["1000", "1500", "2000", "6500"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement