Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. //In this Kata, you will learn how to remove all empty items in an Array.
  2.  
  3. //iterate... if
  4.  
  5. function clean(arr) {
  6.  
  7. arr.map( (elem, i, arr) => {
  8. if(elem === ' '){
  9. arr.splice(i, 1)
  10. }
  11. })
  12.  
  13. return arr;
  14.  
  15. }
  16.  
  17. clean([1, 2,,,3, 4])
  18.  
  19. // In JavaScript, empty items in arrays can be declared by [1, 2,,,3, 4] (Multiple commas without a value in that index)
  20.  
  21. // The values that are not given a value are empty items, and usually add up with others to form <# empty items>; In the example, <2 empty items>
  22.  
  23. // [1, 2, <2 empty items>, 3, 4] should be cleared such that it should be [1, 2, 3, 4]
  24.  
  25. // Example:
  26.  
  27. // Before: [1, 2, 3, <5 empty items>, 4, <1 empty item>, null, undefined];
  28.  
  29. // After: [1, 2, 3, 4, null, undefined];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement