diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-16 13:05:06 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-16 13:05:06 -0400 |
| commit | f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6 (patch) | |
| tree | f601757a365713812e069bd9f49d0ad05046fa41 /challenge-278/zapwai/javascript | |
| parent | 99b5231db016ad59d74ae19ab4f706dab31e9207 (diff) | |
| parent | dd7b7f3344a33d4c8f1b6947e58eeb2ba02f84f3 (diff) | |
| download | perlweeklychallenge-club-f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6.tar.gz perlweeklychallenge-club-f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6.tar.bz2 perlweeklychallenge-club-f1ca8f7ac614d1b7cdc97a4284cfa710ab60f6c6.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-278/zapwai/javascript')
| -rw-r--r-- | challenge-278/zapwai/javascript/ch-1.js | 34 | ||||
| -rw-r--r-- | challenge-278/zapwai/javascript/ch-2.js | 24 |
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=''); +} |
