Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Prompt: write a function that prints every element inside an array containing nested arrays
- var inputArray = [ 1, [2, [3, 4]]];
- function printElements(array) {
- //loop through the outer array
- for (var i = 0; i < array.length; i++) {
- //if the current element is not an array
- if (!Array.isArray(array[i])) {
- //print it
- console.log(array[i]);
- } else {
- //otherwise, call printElements() again passing in the inner array.
- printElements(array[i]);
- }
- }
- }
- printElements(inputArray); //logs: 1 2 3 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement