Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function heartDelivery(input) {
- let index = 0;
- let command = input[index];
- index++;
- let neighbourhood = command.split(`@`).map(Number);
- let houseIndex = 0;
- let houseCount = 0;
- while (command !== "Love!") {
- let jump = input[index].split(" ");
- let jumpLength = Number(jump[1]);
- houseIndex += jumpLength;
- if (houseIndex >= neighbourhood.length) {
- houseIndex = 0;
- }
- if (neighbourhood[houseIndex] === 0) {
- console.log(`Place ${houseIndex} already had Valentine's day.`);
- } else if (neighbourhood[houseIndex] - 2 === 0) {
- neighbourhood[houseIndex] -= 2;
- houseCount++;
- console.log(`Place ${houseIndex} has Valentine's day.`);
- } else {
- neighbourhood[houseIndex] -= 2;
- }
- index++;
- command = input[index];
- }
- console.log(`Cupid's last position was ${houseIndex}.`);
- neighbourhood.length - houseCount === 0 ? console.log("Mission was successful.") :
- console.log(`Cupid has failed ${neighbourhood.length - houseCount} places.`);
- }
- heartDelivery(["10@10@10@2",
- "Jump 1",
- "Jump 2",
- "Love!"]);
- heartDelivery(["2@4@2",
- "Jump 2",
- "Jump 2",
- "Jump 8",
- "Jump 3",
- "Jump 1",
- "Love!"]);
- heartDelivery(["2@2@2",
- "Jump 1",
- "Jump 1",
- "Jump 1",
- "Love!"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement