From 533b4bb643bb83dd14945dd5c648d39b44c9a677 Mon Sep 17 00:00:00 2001 From: itec78 Date: Sun, 14 Mar 2021 02:23:08 +0100 Subject: [PATCH] Added functions to js --- banana.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/banana.js b/banana.js index 1f01165..90523a0 100644 --- a/banana.js +++ b/banana.js @@ -5,6 +5,13 @@ console.log(bananaencode(1000, 8)); console.log(bananarandom()); console.log(bananarandom(10)); +console.log(bananadecode("babaduga")); + +console.log(bananacheck("babaduga")); +console.log(bananacheck("antani")); + + + function bananaencode(num, minlength = 1) { @@ -31,6 +38,58 @@ function bananaencode(num, minlength = 1) { return st; } + + + +function bananadecode(word) { + alphabets = Array("bcdfglmnprstvz".split(""), "aeiou".split("")); + shiftalpha = 0; + alphaend = 0; + + numalpha = alphabets.length; + + if ((word.length - alphaend) % numalpha != 0) { + return "Invalid banana"; + } + v = 0; + + for (i = 0; i < word.length; i++) { + r = (numalpha + i + shiftalpha) % numalpha; + if (alphabets[r].indexOf(word[i]) != -1) { + v = v * alphabets[r].length + alphabets[r].indexOf(word[i]); + } else { + return("Invalid character in position " + (i + 1)); + } + } + + return v; +} + + + +function bananacheck(word) { + alphabets = Array("bcdfglmnprstvz".split(""), "aeiou".split("")); + shiftalpha = 0; + alphaend = 0; + + numalpha = alphabets.length; + + if ((word.length - alphaend) % numalpha != 0) { + return false; + } + + for (i = 0; i < word.length; i++) { + r = (numalpha + i + shiftalpha) % numalpha; + if (alphabets[r].indexOf(word[i]) == -1) { + return false; + } + } + + return true; +} + + + function bananarandom(minlength = 6) { alphabets = Array("bcdfglmnprstvz".split(""), "aeiou".split("")); shiftalpha = 0;