aboutsummaryrefslogtreecommitdiff
path: root/challenge-258/roger-bell-west/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-258/roger-bell-west/javascript')
-rwxr-xr-xchallenge-258/roger-bell-west/javascript/ch-1.js38
-rwxr-xr-xchallenge-258/roger-bell-west/javascript/ch-2.js44
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-258/roger-bell-west/javascript/ch-1.js b/challenge-258/roger-bell-west/javascript/ch-1.js
new file mode 100755
index 0000000000..fb9668f84d
--- /dev/null
+++ b/challenge-258/roger-bell-west/javascript/ch-1.js
@@ -0,0 +1,38 @@
+#! /usr/bin/node
+
+"use strict"
+
+function countevendigitsnumber(a) {
+ let t = 0;
+ for (let p of a) {
+ let even = false;
+ let pt = p;
+ while (pt >= 10) {
+ pt = Math.floor(pt / 10);
+ even = !even;
+ }
+ if (even) {
+ t += 1;
+ }
+ }
+ return t;
+}
+
+if (countevendigitsnumber([10, 1, 111, 24, 1000]) == 3) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (countevendigitsnumber([111, 1, 11111]) == 0) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (countevendigitsnumber([2, 8, 1024, 256]) == 1) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-258/roger-bell-west/javascript/ch-2.js b/challenge-258/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..a065cb06f6
--- /dev/null
+++ b/challenge-258/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,44 @@
+#! /usr/bin/node
+
+"use strict"
+
+function popcount64(x0) {
+ const M1 = 0x5555555555555555n;
+ const M2 = 0x3333333333333333n;
+ const M4 = 0x0f0f0f0f0f0f0f0fn;
+ const H01 = 0x0101010101010101n;
+ let x = BigInt(x0);
+ x -= (x >> 1n) & M1;
+ x = (x & M2) + ((x >> 2n) & M2);
+ x = (x + (x >> 4n)) & M4;
+ return Number((x * H01) >> 56n);
+}
+
+function sumofvalues(a, k) {
+ let s = 0;
+ a.forEach((v, i) => {
+ if (popcount64(i) == k) {
+ s += v;
+ }
+ });
+ return s;
+}
+
+if (sumofvalues([2, 5, 9, 11, 3], 1) == 17) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (sumofvalues([2, 5, 9, 11, 3], 2) == 11) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (sumofvalues([2, 5, 9, 11, 3], 0) == 2) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");