Advertisement
-Enigmos-

heartDelivery.js

Feb 18th, 2022
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function heartDelivery(input) {
  2.     let index = 0;
  3.     let command = input[index];
  4.     index++;
  5.     let neighbourhood = command.split(`@`).map(Number);
  6.     let houseIndex = 0;
  7.     let houseCount = 0;
  8.  
  9.     while (command !== "Love!") {
  10.         let jump = input[index].split(" ");
  11.         let jumpLength = Number(jump[1]);
  12.         houseIndex += jumpLength;
  13.         if (houseIndex >= neighbourhood.length) {
  14.             houseIndex = 0;
  15.         }
  16.  
  17.         if (neighbourhood[houseIndex] === 0) {
  18.             console.log(`Place ${houseIndex} already had Valentine's day.`);
  19.        } else if (neighbourhood[houseIndex] - 2 === 0) {
  20.            neighbourhood[houseIndex] -= 2;
  21.            houseCount++;
  22.            console.log(`Place ${houseIndex} has Valentine's day.`);
  23.         } else {
  24.             neighbourhood[houseIndex] -= 2;
  25.         }
  26.  
  27.         index++;
  28.         command = input[index];
  29.     }
  30.  
  31.     console.log(`Cupid's last position was ${houseIndex}.`);
  32.    neighbourhood.length - houseCount === 0 ? console.log("Mission was successful.") :
  33.    console.log(`Cupid has failed ${neighbourhood.length - houseCount} places.`);
  34. }
  35.  
  36. heartDelivery(["10@10@10@2",
  37. "Jump 1",
  38. "Jump 2",
  39. "Love!"]);
  40. heartDelivery(["2@4@2",
  41. "Jump 2",
  42. "Jump 2",
  43. "Jump 8",
  44. "Jump 3",
  45. "Jump 1",
  46. "Love!"]);
  47. heartDelivery(["2@2@2",
  48. "Jump 1",
  49. "Jump 1",
  50. "Jump 1",
  51. "Love!"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement