marinvch

2. Add and Remove Elements from Array

Oct 20th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let modified = []
  3.     let counter = 1;
  4.     for (let i = 0; i < arr.length; i++) {
  5.         if (arr[i] === 'add') {
  6.             modified.push(counter++)
  7.  
  8.         } else if (arr[i] === 'remove') {
  9.             modified.pop(counter++)
  10.         }
  11.  
  12.     }
  13.  
  14.     if (modified[0] === undefined) {
  15.         console.log('Empty')
  16.     } else {
  17.         console.log(modified.join(' '));
  18.     }
  19.  
  20. }
Add Comment
Please, Sign In to add comment