SHARE
TWEET

Untitled

a guest Feb 29th, 2020 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(a, b) {
  2.     let arr = a;
  3.     let comands = b;
  4.     for (let i = 0; i < comands.length; i++) {
  5.         let comand = comands[i].split(` `);
  6.         let action = comand.shift();
  7.         if (action === `add`) {
  8.             let index = comand.shift()
  9.             let element = Number(comand.shift())
  10.             arr.splice(index, 0, element)
  11.         } else if (action === `addMany`) {
  12.             let index = comand.shift();
  13.             let elements = comand.map(Number)
  14.             arr.splice(index, 0, ...elements)
  15.         } else if (action === `contains`) {
  16.             let element = Number(comand.shift())
  17.             let findIndex = arr.indexOf(element)
  18.             console.log(findIndex)
  19.         } else if (action === `remove`) {
  20.             let index = Number(comand.shift())
  21.             arr.splice(index, 1)
  22.         } else if (action === `shift`) {
  23.             let positionToShift = Number(comand.shift())
  24.             let elements = arr.splice(0, positionToShift)
  25.             arr.push(...elements)
  26.         } else if (action === `sumPairs`) {
  27.             let newArr = [];
  28.             for (let i = 0; i < arr.length; i++) {
  29.                 if (i === arr.length - 1) {
  30.                     newArr.push(arr[i])
  31.                 } else {
  32.                     newArr.push(arr[i] + arr[i + 1])
  33.                     i++
  34.                 }
  35.             }
  36.             arr = newArr;
  37.         } else if (action === `print`) {
  38.             console.log(`[ ${arr.join(`, `)} ]`)
  39.         } else {
  40.             console.log(`[ ${arr.join(`, `)} ]`)
  41.         }
  42.     }
  43. }
  44. solve([1, 2, 3, 4, 5], ['addMany 5 9 8 7 6 5', 'contains 15', 'remove 3', 'shift 1', 'print'])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top