Liliana797979

heart delivery1 mid exam - fundamentals

Jun 30th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function foo(arr) {
  3.     arr = arr.filter(x => x !== "")
  4.     let  field = arr.shift().split("@").map(Number)
  5.     arr.pop()
  6.     arr = arr.map(x => x.split(" ")[1]).map(Number)
  7.     let currentIndex = 0
  8.  
  9.     for (let i = 0; i < arr.length; i++) {
  10.         if (currentIndex + arr[i] >= field.length) {
  11.             currentIndex = 0
  12.         } else {
  13.             currentIndex += arr[i]
  14.         }
  15.  
  16.         if (field[currentIndex] === 0) {
  17.             console.log(`Place ${currentIndex} already had Valentine's day.`)
  18.            continue
  19.        }
  20.        field[currentIndex] -= 2
  21.        if (field[currentIndex] === 0) {
  22.            console.log(`Place ${currentIndex} has Valentine's day.`)
  23.         }
  24.     }
  25.  
  26.     console.log(`Cupid's last position was ${currentIndex}.`)
  27.    field.every(x => x === 0)
  28.        ? console.log(`Mission was successful.`)
  29.        : console.log(
  30.            `Cupid has failed ${field.filter(x => x !== 0).length} places.`
  31.        )
  32. }
  33.  
  34. foo(['2@4@2', 'Jump 2', 'Jump 2', 'Jump 8', 'Jump 3', 'Jump 1', 'Love!'])
Advertisement
Add Comment
Please, Sign In to add comment