aboutsummaryrefslogtreecommitdiff
path: root/challenge-243/bruce-gray/javascript/ch-2.js
diff options
context:
space:
mode:
authorLakpa Tashi Bhutia <lakpatashi@users.noreply.github.com>2023-11-25 21:02:55 +0530
committerGitHub <noreply@github.com>2023-11-25 21:02:55 +0530
commit6db863c3ee8cb8a3315eeae5fde99af83385aefc (patch)
tree761f0c9b8aef13375cca819d8deed4a70fff8c7e /challenge-243/bruce-gray/javascript/ch-2.js
parent9c96baeeda85997b321a4d4fbebd5fd51bffb729 (diff)
parent9a1dd9390973363a40ccda4a6d03f9f61a0ae386 (diff)
downloadperlweeklychallenge-club-6db863c3ee8cb8a3315eeae5fde99af83385aefc.tar.gz
perlweeklychallenge-club-6db863c3ee8cb8a3315eeae5fde99af83385aefc.tar.bz2
perlweeklychallenge-club-6db863c3ee8cb8a3315eeae5fde99af83385aefc.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-243/bruce-gray/javascript/ch-2.js')
-rw-r--r--challenge-243/bruce-gray/javascript/ch-2.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-243/bruce-gray/javascript/ch-2.js b/challenge-243/bruce-gray/javascript/ch-2.js
new file mode 100644
index 0000000000..c5664a5c35
--- /dev/null
+++ b/challenge-243/bruce-gray/javascript/ch-2.js
@@ -0,0 +1,54 @@
+// Procedural
+function task2a ( ns ) {
+ let ret = 0;
+ for ( const x of ns ) {
+ for ( const y of ns ) {
+ ret += Math.floor( x / y );
+ }
+ }
+ return ret;
+}
+
+// Functional, both using extracted code for Cartesian product.
+const cartesian =
+ (...top_level_arrays) => top_level_arrays.reduce(
+ (accum, an_array) => accum.flatMap(
+ (element1) => an_array.map(
+ (element2) => [element1, element2].flat()
+)));
+
+// .map(), then sum via .reduce .
+function task2b (ns) {
+ return cartesian(ns, ns)
+ .map(([x,y]) => Math.floor(x/y))
+ .reduce(((a,b) => a+b),0);
+}
+
+// Do the summing *and* mapping during the .reduce .
+const task2c = (ns) => cartesian(ns, ns).reduce(((a,[x,y]) => a+Math.floor(x/y)),0);
+
+
+
+let test_number = 0;
+function is ( got, expected, desc ) {
+ test_number++;
+ const ok_msg = (got === expected) ? "ok" : "not ok";
+ const description = (typeof desc !== 'undefined') ? ` - ${desc}` : '';
+ console.log(`${ok_msg} ${test_number}${description}`);
+}
+const subs = [
+ [ 'task2a', task2a ],
+ [ 'task2b', task2b ],
+ [ 'task2c', task2c ],
+];
+const tests = [
+ [ 10, [2, 5, 9] , 'Example 1 from task'],
+ [ 49, [7,7,7,7,7,7,7], 'Example 2 from task'],
+ [ 10, [2, 5, 9] , 'Null array'],
+];
+for ( const [ sub_name, task2_coderef ] of subs ) {
+ for ( const [ expected, input, test_name ] of tests ) {
+ const got = task2_coderef(input);
+ is( got, expected, `${sub_name}: ${test_name}`);
+ }
+}