Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let neighbourhood = arr.shift().split('@').map(Number);
- let missionSuccessful = false;
- let command = arr.shift();
- let housesCount = neighbourhood.length;
- let currentIndex = 0;
- let housesLeft = neighbourhood.length;
- while(command !== 'Love!') {
- let commandArr = command.split(' ');
- currentIndex += Number(commandArr[1]);
- if(currentIndex >= housesCount) {
- currentIndex = 0;
- }
- if(neighbourhood[currentIndex] <= 0) {
- console.log(`Place ${currentIndex} already had Valentine's day.`);
- } else {
- neighbourhood[currentIndex] -= 2;
- if(neighbourhood[currentIndex] <= 0) {
- console.log(`Place ${currentIndex} has Valentine's day.`);
- housesLeft--;
- }
- }
- command = arr.shift();
- }
- console.log(`Cupid's last position was ${currentIndex}.`);
- if(missionSuccessful) {
- console.log(`Mission was successful.`);
- } else {
- console.log(`Cupid has failed ${housesLeft} places.`);
- }
- }
Add Comment
Please, Sign In to add comment