Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Note: This is coded in Javascript, ES6.
- // Declare control variables.
- 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];
- let pure = [];
- // Declare recursive function for checking item.
- function checkItem(item) {
- // If item is array, check each item in array.
- if (Array.isArray(item)) {
- for (let i = 0; i < item.length; i++) {
- checkItem(item[i]);
- }
- }
- // If item is not a number, notify user.
- else if (isNaN(item) || item === undefined || item === null || item === true || item === false) {
- console.log(`\nItem is not a number: ${item}`);
- }
- // If item is number, append to array.
- else {
- pure.push(item);
- }
- }
- // Check nested array, then log results.
- checkItem(nest);
- console.log(`\nData: ${pure}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement