Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. // Note: This is coded in Javascript, ES6.
  2.  
  3. // Declare control variables.
  4. const nest = [9, 8, [7, null, 6, [5, 4, [3, true], 2], 1, false, 'a'], 0, [-1, -2], [-3, ['b', -4, [-5, [-6, 'c']], -7], undefined, -8], -9];
  5. let pure = [];
  6.  
  7. // Declare recursive function for checking item.
  8. function checkItem(item) {
  9. // If item is array, check each item in array.
  10. if (Array.isArray(item)) {
  11. for (let i = 0; i < item.length; i++) {
  12. checkItem(item[i]);
  13. }
  14. }
  15. // If item is not a number, notify user.
  16. else if (isNaN(item) || item === undefined || item === null || item === true || item === false) {
  17. console.log(`\nItem is not a number: ${item}`);
  18. }
  19. // If item is number, append to array.
  20. else {
  21. pure.push(item);
  22. }
  23. }
  24.  
  25. // Check nested array, then log results.
  26. checkItem(nest);
  27. console.log(`\nData: ${pure}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement