Advertisement
Spocoman

03. Heart Delivery

Nov 9th, 2023
883
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function heartDelivery(input) {
  2.     let houses = input.shift().split('@').map(Number);
  3.     let index = 0;
  4.  
  5.     while (input[0] !== 'Love!') {
  6.         index += Number(input.shift().slice(5));
  7.         if (index >= houses.length){
  8.             index = 0;
  9.         }
  10.        
  11.         if (houses[index] == 0) {
  12.             console.log(`Place ${index} already had Valentine's day.`);
  13.        } else {
  14.            houses[index] -= 2;
  15.            if (houses[index] == 0) {
  16.                console.log(`Place ${index} has Valentine's day.`);
  17.             }
  18.         }
  19.     }
  20.  
  21.     console.log(`Cupid's last position was ${index}.`);
  22.    let houseCount = houses.filter(x => x !== 0).length;
  23.    console.log(houseCount === 0 ? 'Mission was successful.' : `Cupid has failed ${houseCount} places.`);
  24.    return;
  25. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement