diff options
| author | Roger Bell_West <roger@firedrake.org> | 2024-02-27 08:25:42 +0000 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2024-02-27 08:25:42 +0000 |
| commit | 38de97ad51e196b9cc76a803bc3600f45c8615b4 (patch) | |
| tree | 048dc86bd4c0cfa549db5617f3a76d59da300d71 /challenge-258/roger-bell-west/javascript | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-38de97ad51e196b9cc76a803bc3600f45c8615b4.tar.gz perlweeklychallenge-club-38de97ad51e196b9cc76a803bc3600f45c8615b4.tar.bz2 perlweeklychallenge-club-38de97ad51e196b9cc76a803bc3600f45c8615b4.zip | |
RogerBW solutions for challenge no. 258
Diffstat (limited to 'challenge-258/roger-bell-west/javascript')
| -rwxr-xr-x | challenge-258/roger-bell-west/javascript/ch-1.js | 38 | ||||
| -rwxr-xr-x | challenge-258/roger-bell-west/javascript/ch-2.js | 44 |
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"); |
