Advertisement
teofarov13

Untitled

Feb 6th, 2023
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addAndRemove(arr) {
  2.     let spliced = 0;
  3.     let currCommand;
  4.     let num = 1;
  5.     let tempArr = [];
  6.     for (let index = 0; index < arr.length; index++) {
  7.         currCommand = arr[index];
  8.         if (currCommand === 'add') {
  9.             currCommand = num;
  10.             tempArr.push(currCommand)
  11.  
  12.         } if (currCommand === "remove") {
  13.             tempArr.splice(spliced - 1);
  14.             // tempArr.push(num)
  15.         }
  16.         num++;
  17.         spliced++
  18.     }
  19.     arr=tempArr
  20.     if (!arr.length) {
  21.         console.log(`Empty`)
  22.     } else {
  23.         console.log(`${arr.join(" ")}`)
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement