aboutsummaryrefslogtreecommitdiff
path: root/challenge-271/zapwai/javascript/ch-2.js
diff options
context:
space:
mode:
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);
+}