Ivankooo1

02.Add and Remove

Aug 29th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function addAndRemove(arr) {
  2.  
  3.     let result = [];
  4.  
  5.     for (let i = 0; i < arr.length; i++) {
  6.  
  7.         if (arr[i] === 'remove') {
  8.  
  9.             result.pop();
  10.  
  11.         } else if (arr[i] === 'add') {
  12.  
  13.             result.push(i + 1);
  14.  
  15.         }
  16.  
  17.     }
  18.  
  19.  
  20.  
  21.     if (result.length === 0) {
  22.  
  23.         console.log('Empty');
  24.  
  25.     } else {
  26.  
  27.         console.log(result.join(' '));
  28.  
  29.     }
  30.  
  31. }
  32.  addAndRemove(['add','add','remove','add','add']);
Advertisement
Add Comment
Please, Sign In to add comment