Skip to content

Commit

Permalink
Morse fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ghluka committed Apr 28, 2024
1 parent 9fbf320 commit 6019bdf
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions techdemos/encode/js/encoders/morse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ var morse = {".": ".-.-.-", ",": "--..--", "?": "..--..", "'": ".----.", "/": "-

function encodeMorse(text) {
text = text.toLowerCase();
var encoded_text = ""

for (const [key, value] of Object.entries(morse)) {
text = text.replaceAll(key, value + " ");
for (const character of text.split("")) {
for (const [key, value] of Object.entries(morse)) {
if (character == key) {
encoded_text += value + " ";
}
}
}

text = text.substring(0, text.length - 1);
encoded_text = encoded_text.substring(0, encoded_text.length - 1);

return text;
return encoded_text;
}

function decodeMorse(code) {
Expand Down

0 comments on commit 6019bdf

Please sign in to comment.