Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(array) {
  2.  
  3.     let list = array.shift().split(' ')
  4.  
  5.     for (let i = 0; i < array.length; i++) {
  6.  
  7.         let element = array[i].split(' ')
  8.         let command = element[0]
  9.         let present = element[1]
  10.  
  11.         if (command === 'No Money'){
  12.             break;
  13.         }
  14.  
  15.         if (command === 'OutOfStock') {
  16.             let count = list.filter(item => item === present).length;
  17.             for (let i = 0; i < count; i++) {
  18.                 let index = list.indexOf(present)
  19.                 let none = 'None'
  20.                 list.splice(index, 1, none)
  21.  
  22.             }
  23.  
  24.         } else if (command === 'Required') {
  25.             let index = element[2]
  26.             if (index >= 0 && index <= list.length-1) {
  27.                 list.splice(index, 1, present)
  28.             }
  29.         } else if (command === 'JustInCase') {
  30.            list.splice(list.length - 1, 1, present)
  31.         }
  32.  
  33.     }
  34.  
  35.     let filtred = list.filter(item => item !== 'None')
  36.  
  37.     console.log(filtred.join(' '))
  38.    
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement