Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function walking(input) {
- let stepSum = 0;
- for (let i = 0; i < input.length; i++){
- if (input[i] !== "Going home"){
- stepSum += Number(input[i]);
- }
- }
- if (stepSum < 10000) {
- console.log(`${10000 - stepSum} more steps to reach goal.`);
- } else {
- console.log(`Goal reached! Good job!\n${stepSum - 10000} steps over the goal!`);
- }
- }
- Решение с тернарен оператор:
- function walking(input) {
- let stepSum = 10000;
- for (let i = 0; i < input.length; i++) {
- stepSum -= input[i] !== "Going home" ? Number(input[i]) : 0;
- }
- console.log(stepSum > 0 ? `${stepSum} more steps to reach goal.` :
- `Goal reached! Good job!\n${Math.abs(stepSum)} steps over the goal!`);
- }
- Или с while():
- function walking(input) {
- stepSum = 10000;
- while (stepSum > 0) {
- if (input[0] === "Going home"){
- stepSum -= Number(input[1]);
- break;
- }
- stepSum -= Number(input.shift());
- }
- console.log(stepSum > 0 ? `${stepSum} more steps to reach goal.`
- : `Goal reached! Good job!\n${Math.abs(stepSum)} steps over the goal!`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement