kstoyanov

03. Add and Remove Elements

Sep 18th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.   const result = [];
  3.  
  4.   arr.forEach((_, index) => {
  5.     if (arr[index] === 'add') {
  6.       result[index] = index + 1;
  7.     } else if (arr[index] === 'remove') {
  8.       result.pop();
  9.     }
  10.   });
  11.  
  12.   if (result.length <= 0) {
  13.     console.log('Empty');
  14.   }
  15.  
  16.   result.map((el) => el !== undefined && console.log(el));
  17. }
Advertisement
Add Comment
Please, Sign In to add comment