Advertisement
dimoBs

heart delivery

Feb 23rd, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //heart delivery
  2. function solve(array) {
  3.  
  4.    let position = 0;
  5.    let count = 0;
  6.     let index = 0;
  7.     let slice = 0;
  8.     let neighborhood = array.shift().split('@');
  9.  
  10.        for (let i = 0; i < array.length; i ++ ){
  11.         command = array[i];
  12.  
  13.         while (command !== 'Love!'){
  14.              let [jumps, step] = command.split(' ');
  15.               position = Number(step) + Number(index)
  16.                
  17.             if(neighborhood.length <= position){
  18.                 index = 0;
  19.                 position = 0;
  20.                 neighborhood[index] -= 2;
  21.                slice = Number(neighborhood[index]);
  22.  
  23.                if(count >= 1){
  24.                console.log(`Place ${position} already had Valentine's day.`);
  25.  
  26.               }else {
  27.                console.log(`Place ${position} has Valentine's day.`);
  28.                }
  29.                count++
  30.                break;
  31.  
  32.             }else{
  33.                 let positive = Number(neighborhood[position]);          
  34.                 if(positive > 0){
  35.                neighborhood[Number(step) + Number(index)] -= 2;
  36.              slice = Number(neighborhood[position]);
  37.           index = step;
  38.                 }
  39.             }
  40.            if(slice <= 0){
  41.                    console.log(`Place ${position} has Valentine's day.`);
  42.           }            
  43.        break;
  44.        }
  45.    }
  46.    console.log(`Cupid's last position was ${position}.`);
  47.     console.log(`Cupid has failed ${position} places.`);
  48. }
  49. solve([ '10@10@10@2', 'Jump 1', 'Jump 2', 'Love!' ])
  50. solve(["2@4@2","Jump 2","Jump 2","Jump 8","Jump 3","Jump 1","Love!"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement