Skip to content

Instantly share code, notes, and snippets.

@onionmk2
Last active January 4, 2017 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onionmk2/49c424332fb53d43319f733043d19a32 to your computer and use it in GitHub Desktop.
Save onionmk2/49c424332fb53d43319f733043d19a32 to your computer and use it in GitHub Desktop.
recursive function call
const arr =
[
[
[1, 2, 3],
[4, 5, 6],
],
[
[7, 8, 9],
[10, 11, 12],
]
];
const isNumberArray = (arr) => {
return arr.every(item => {
return typeof(item) === 'number' && !Number.isNaN(item);
})
};
const walk1 = (arr) => {
if (isNumberArray(arr)) {
console.log(`arr is [${arr}]. walk ended.`);
} else {
for (const a of arr) {
walk1(a);
}
}
}
function walk2(arr) {
if (isNumberArray(arr)) {
console.log(`arr is [${arr}]. walk ended.`);
} else {
for (const a of arr) {
walk2(a);
}
}
}
walk1(arr);
walk2(arr);
@onionmk2
Copy link
Author

onionmk2 commented Jan 4, 2017

const walk = function() { 
  // something
  walk();  
}
function walk() { 
  // something
  walk();
}

どちらでも再帰できる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment