Advertisement
Spocoman

High Jump

Feb 23rd, 2022 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function highJumps(input) {
  2.     let index = 0;
  3.     let jumpTarget = Number(input[index++]);
  4.     let startJump = jumpTarget - 30;
  5.     let counter = 0;
  6.     let fall = 0;
  7.  
  8.     while (fall !== 3 && startJump <= jumpTarget) {
  9.         let jump = Number(input[index++]);
  10.  
  11.         if (jump > startJump) {
  12.             startJump += 5;
  13.             fall = 0;
  14.         } else {
  15.             fall++;
  16.         }
  17.         counter++;
  18.     }
  19.  
  20.     if (startJump <= jumpTarget) {
  21.         console.log(`Tihomir failed at ${startJump}cm after ${counter} jumps.`);
  22.     } else {
  23.         console.log(`Tihomir succeeded, he jumped over ${jumpTarget}cm after ${counter} jumps.`);
  24.     }
  25. }
  26.  
  27. РЕШЕНИЕ СЪС SHIFT():
  28.  
  29. function highJumps(input) {
  30.     let jumpTarget = Number(input.shift());
  31.     let startJump = jumpTarget - 30;
  32.     let counter = 0;
  33.     let fall = 0;
  34.  
  35.     while (fall !== 3 && startJump <= jumpTarget) {
  36.         let jump = Number(input.shift());
  37.  
  38.         if (jump > startJump) {
  39.             startJump += 5;
  40.             fall = 0;
  41.         } else {
  42.             fall++;
  43.         }
  44.         counter++;
  45.     }
  46.  
  47.     if (startJump <= jumpTarget) {
  48.         console.log(`Tihomir failed at ${startJump}cm after ${counter} jumps.`);
  49.     } else {
  50.         console.log(`Tihomir succeeded, he jumped over ${jumpTarget}cm after ${counter} jumps.`);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement