aboutsummaryrefslogtreecommitdiff
path: root/challenge-271/zapwai/javascript/ch-2.js
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-27 11:52:22 +0100
committerGitHub <noreply@github.com>2024-05-27 11:52:22 +0100
commit7a80978d123027cd31180b1b9300b2a4e04dfada (patch)
treefa25c3550b3b050d9ac49f17956db78eb7d8b6c9 /challenge-271/zapwai/javascript/ch-2.js
parent6bdea3bf77857833e97cf55de053444723bb31c6 (diff)
parentcc7611c673901442ddfac5a71c691350144dcd03 (diff)
downloadperlweeklychallenge-club-7a80978d123027cd31180b1b9300b2a4e04dfada.tar.gz
perlweeklychallenge-club-7a80978d123027cd31180b1b9300b2a4e04dfada.tar.bz2
perlweeklychallenge-club-7a80978d123027cd31180b1b9300b2a4e04dfada.zip
Merge pull request #10157 from zapwai/branch-for-271
Week 271
Diffstat (limited to 'challenge-271/zapwai/javascript/ch-2.js')
-rw-r--r--challenge-271/zapwai/javascript/ch-2.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-271/zapwai/javascript/ch-2.js b/challenge-271/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..7f4dc63a58
--- /dev/null
+++ b/challenge-271/zapwai/javascript/ch-2.js
@@ -0,0 +1,54 @@
+let ints = [0, 1, 2, 3, 4, 5, 6, 7, 8];
+proc(ints);
+ints = [1024, 512, 256, 128, 64];
+proc(ints);
+function proc(ints) {
+ console.log( "Input: ", ints);
+ let count = [];
+ for (let i of ints) {
+ let bin = i.toString(2);
+ let dig = bin.split("");
+ let cnt = 0;
+ for (let d of dig) {
+ if (d == "1") {
+ cnt++;
+ }
+ }
+ count.push(cnt);
+ }
+ let ord = ints;
+ let c = 1;
+ while (c != 0) {
+ c = 0;
+ for (let i = 0; i < ord.length - 1; i++) {
+ if (count[i] > count[i+1]) {
+ c++;
+ let tmp_cnt = count[i];
+ let tmp_int = ord[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ }
+ }
+ }
+ c = 1;
+ while (c != 0) {
+ c = 0;
+ for (let i = 0; i < ord.length - 1; i++) {
+ if (count[i] != count[i+1]) {
+ continue;
+ }
+ if (ord[i] > ord[i+1]) {
+ c++;
+ let tmp_int = ord[i];
+ ord[i] = ord[i+1];
+ ord[i+1] = tmp_int;
+ let tmp_cnt = count[i];
+ count[i] = count[i+1];
+ count[i+1] = tmp_cnt;
+ }
+ }
+ }
+ console.log( "Output:",ord);
+}