aboutsummaryrefslogtreecommitdiff
path: root/challenge-278/zapwai/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-278/zapwai/javascript')
-rw-r--r--challenge-278/zapwai/javascript/ch-1.js34
-rw-r--r--challenge-278/zapwai/javascript/ch-2.js24
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-278/zapwai/javascript/ch-1.js b/challenge-278/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..a399c8ee21
--- /dev/null
+++ b/challenge-278/zapwai/javascript/ch-1.js
@@ -0,0 +1,34 @@
+let str = "and2 Raku3 cousins5 Perl1 are4";
+proc(str);
+str = "guest6 Python1 most4 the3 popular5 is2 language7";
+proc(str);
+str = "Challenge3 The1 Weekly2";
+proc(str);
+
+function proc(str) {
+ console.log("Input:", str);
+ let words = [];
+ let keys = [];
+ for (let word of str.split(" ")) {
+ let key = word.substr(-1);
+ let w = word.substr(0, word.length - 1);
+ words.push(w);
+ keys.push(key);
+ }
+ let cnt = 1;
+ while (cnt > 0) {
+ cnt = 0;
+ for (let i = 0; i < words.length - 1; i++) {
+ if (keys[i] > keys[i + 1]) {
+ let keynum = keys[i];
+ keys[i] = keys[i + 1];
+ keys[i + 1] = keynum;
+ let word = words[i];
+ words[i] = words[i + 1];
+ words[i + 1] = word;
+ cnt++;
+ }
+ }
+ }
+ console.log( "Output:",words);
+}
diff --git a/challenge-278/zapwai/javascript/ch-2.js b/challenge-278/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..1753075d96
--- /dev/null
+++ b/challenge-278/zapwai/javascript/ch-2.js
@@ -0,0 +1,24 @@
+let str = "challenge";
+let mychar = "e";
+proc(str, mychar);
+str = "programming";
+mychar = "a";
+proc(str, mychar);
+str = "champion";
+mychar = "b";
+proc(str, mychar);
+function proc(str, mychar) {
+ console.log("Input: str =", str, "mychar =", mychar);
+ let ind = str.indexOf(mychar);
+ if (ind == -1) {
+ console.log("Output:",str);
+ } else {
+ let begin = str.substr(0, ind + 1);
+ let endy = str.substr(ind + 1);
+ console.log(arrange(begin)+endy);
+ }
+}
+function arrange(word) {
+ let arr = word.split("");
+ return arr.sort().join(separator='');
+}