Liliana797979

heart delivery mid exam - fundamentals

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