From 8e42048be00ec7d445174dc9df264105252ea247 Mon Sep 17 00:00:00 2001 From: Manuel Barzi Date: Thu, 3 Oct 2024 13:19:14 +0200 Subject: [PATCH] add recursion demo #163 --- stuff/js/recursion/for-each.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 stuff/js/recursion/for-each.js diff --git a/stuff/js/recursion/for-each.js b/stuff/js/recursion/for-each.js new file mode 100644 index 00000000..0247b389 --- /dev/null +++ b/stuff/js/recursion/for-each.js @@ -0,0 +1,19 @@ +var ns = [100, 200, 300] + +/* +for (var i = 0; i < ns.length; i++) + console.log(ns[i]) +*/ + +//ns.forEach(function(n) { console.log(n) }) + +var forEach = function (array, index) { + if (index === undefined) index = 0 + + console.log(array[index]) + + if (index < array.length - 1) + forEach(array, index + 1) +} + +forEach(ns) \ No newline at end of file