Advertisement
dabidabidesh

addAndRemove

Jun 5th, 2020
1,627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addAndRemove0(arr) {
  2.   'use strict'
  3.  
  4.   let workArr = arr
  5.   let outputArr = []
  6.   let index = 0
  7.  
  8.   for (let i = 0; i < workArr.length; i++) {
  9.     if (workArr[i] === 'add') {
  10.       outputArr[index] = (i + 1)
  11.       index++
  12.     }
  13.     else if (workArr[i] === 'remove') {
  14.       if (index !== 0) {
  15.         outputArr[index - 1] = undefined
  16.         index--
  17.       }
  18.       else
  19.         outputArr[0] = undefined
  20.     }
  21.   }
  22.  
  23.   let output = ''
  24.  
  25.   let lengthArray = 0
  26.   while (outputArr[lengthArray] !== undefined) {
  27.     lengthArray++
  28.   }
  29.  
  30.   for (let i = 0; i < lengthArray; i++)
  31.     output += outputArr[i] + ' '
  32.   if (lengthArray === 0)
  33.     console.log('Empty')
  34.   else
  35.     console.log(output)
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement