Liliana797979

heart delivery mid exam - fundamentals

Jun 30th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function solve(input) {
  3.     // parse input
  4.     let houses = input.shift().split('@').map(Number);
  5.     let index = 0;
  6.  
  7.     // for each remaining element, before 'Love!'
  8.     for (let line of input) {
  9.         if (line == 'Love!') {
  10.             break;
  11.         }
  12.  
  13.         // - parse command
  14.         let offset = Number(line.split(' ')[1]);
  15.  
  16.         // - move index
  17.         index += offset;
  18.  
  19.         // - if index outside array -> index = 0
  20.         if (index >= houses.length) {
  21.             index = 0;
  22.         }
  23.  
  24.         // - if heart count = 0, print message
  25.         if (houses[index] == 0) {
  26.             console.log(`Place ${index} already had Valentine's day.`);
  27.        } else {
  28.            // - else decrease heart count and check if heart count = 0
  29.            houses[index] -= 2;
  30.            if (houses[index] == 0) {
  31.                console.log(`Place ${index} has Valentine's day.`);
  32.             }
  33.         }
  34.     }
  35.  
  36.  
  37.     // print result
  38.     console.log(`Cupid's last position was ${index}.`);
  39.    let missed = houses.filter(x => x > 0).length;
  40.    if (missed == 0) {
  41.        console.log('Mission was successful.');
  42.    } else {
  43.        console.log(`Cupid has failed ${missed} places.`);
  44.    }
  45. }
  46.  
  47. solve(['10@10@10@2', 'Jump 1', 'Jump 2', 'Love!']);
  48. solve(['2@4@2', 'Jump 2', 'Jump 2', 'Jump 8', 'Jump 3', 'Jump 1', 'Love!']);
Advertisement
Add Comment
Please, Sign In to add comment