RRusev77

HeartDelivery

Jun 28th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let neighbourhood = arr.shift().split('@').map(Number);
  3.     let missionSuccessful = false;
  4.     let command = arr.shift();
  5.     let housesCount = neighbourhood.length;
  6.     let currentIndex = 0;
  7.     let housesLeft = neighbourhood.length;
  8.  
  9.     while(command !== 'Love!') {
  10.         let commandArr = command.split(' ');
  11.  
  12.         currentIndex += Number(commandArr[1]);
  13.         if(currentIndex >= housesCount) {
  14.             currentIndex = 0;
  15.         }
  16.        
  17.         if(neighbourhood[currentIndex] <= 0) {
  18.             console.log(`Place ${currentIndex} already had Valentine's day.`);
  19.        } else {
  20.            neighbourhood[currentIndex] -= 2;
  21.            if(neighbourhood[currentIndex] <= 0) {
  22.                console.log(`Place ${currentIndex} has Valentine's day.`);
  23.                 housesLeft--;
  24.             }
  25.         }
  26.  
  27.         command = arr.shift();
  28.     }
  29.  
  30.     console.log(`Cupid's last position was ${currentIndex}.`);
  31.  
  32.    if(missionSuccessful) {
  33.        console.log(`Mission was successful.`);
  34.    } else {
  35.        console.log(`Cupid has failed ${housesLeft} places.`);
  36.    }
  37. }
Add Comment
Please, Sign In to add comment