Advertisement
Guest User

Untitled

a guest
Feb 9th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function heartDelivery(arrOfElements) {
  2.   let neighborhood = arrOfElements.shift().split("@").map(Number);
  3.   let actions = arrOfElements.shift().split(" ");
  4.   let currentCommand = actions.shift();
  5.   let previousIndex = 0;
  6.  
  7.   while (currentCommand !== "Love!") {
  8.     let jumpIndex = Number(actions.shift());
  9.     jumpIndex += previousIndex;
  10.  
  11.     if (jumpIndex >= neighborhood.length || jumpIndex < 0) {
  12.       jumpIndex = 0;
  13.     }
  14.  
  15.     if (neighborhood[jumpIndex] !== 0) {
  16.       neighborhood[jumpIndex] -= 2;
  17.       if (neighborhood[jumpIndex] <= 0) {
  18.         console.log(`Place ${jumpIndex} has Valentine's day.`);
  19.      }
  20.    } else {
  21.      console.log(`Place ${jumpIndex} already had Valentine's day.`);
  22.     }
  23.     previousIndex = jumpIndex;
  24.     actions = arrOfElements.shift().split(" ");
  25.     currentCommand = actions.shift();
  26.   }
  27.  
  28.   let failedPlaces = neighborhood.filter((x) => x !== 0);
  29.   let successPlaces = neighborhood.filter((y) => y === 0);
  30.  
  31.   console.log(`Cupid's last position was ${previousIndex}.`);
  32.  if (successPlaces.length === neighborhood.length) {
  33.    return console.log(`Mission was successful.`);
  34.  } else {
  35.    //console.log(`Cupid's last position was ${previousIndex}.`);
  36.     console.log(`Cupid has failed ${failedPlaces.length} places.`);
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement