Liliana797979

cupidon - mid exam - fundamentals

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