Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

desafio 10 pedro pizzo #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions javascript/desafio-10/@plowzzer-pedro_pizzo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// On Codepen: https://codepen.io/plowzzer/pen/KKdrePr
// Codificador de Código Morse

function morseEncode(string) {
const stringSplit = string
.normalize("NFD") // normalizing the code
.replace(/[\u0300-\u036f]/g, "")
.split(" ")
.join("_")
.toUpperCase()
.split("");

const returnCode = [];
let result = "";

stringSplit.forEach((key) => {
switch (key) {
case "A":
returnCode.push(".-");
break;
case "B":
returnCode.push("-...");
break;
case "C":
returnCode.push("-.-.");
break;
case "D":
returnCode.push("-..");
break;
case "E":
returnCode.push(".");
break;
case "F":
returnCode.push("..-.");
break;
case "G":
returnCode.push("--.");
break;
case "H":
returnCode.push("....");
break;
case "I":
returnCode.push("..");
break;
case "J":
returnCode.push(".---");
break;
case "K":
returnCode.push("-.-");
break;
case "L":
returnCode.push(".-..");
break;
case "M":
returnCode.push("--");
break;
case "N":
returnCode.push("-.");
break;
case "O":
returnCode.push("---");
break;
case "P":
returnCode.push(".--.");
break;
case "Q":
returnCode.push("--.-");
break;
case "R":
returnCode.push(".-.");
break;
case "S":
returnCode.push("...");
break;
case "T":
returnCode.push("-");
break;
case "U":
returnCode.push("..-");
break;
case "V":
returnCode.push("...-");
break;
case "W":
returnCode.push(".--");
break;
case "X":
returnCode.push("-..-");
break;
case "Y":
returnCode.push("-.--");
break;
case "Z":
returnCode.push("--..");
break;
case "0":
returnCode.push("-----");
break;
case "1":
returnCode.push("·----");
break;
case "2":
returnCode.push("·.---");
break;
case "3":
returnCode.push("·..--");
break;
case "3":
returnCode.push("·...-");
break;
case "4":
returnCode.push("·...-");
break;
case "5":
returnCode.push("·....");
break;
case "6":
returnCode.push("-....");
case "7":
returnCode.push("--...");
break;
case "8":
returnCode.push("---..");
break;
case "9":
returnCode.push("----.");
break;
case ".":
returnCode.push("·-·-·-");
break;
case ",":
returnCode.push("--..--");
break;
case "?":
returnCode.push("..--..");
break;
case "'":
returnCode.push(".----.");
break;
case "!":
returnCode.push("-.-.--");
break;
case "/":
returnCode.push("-··-·");
break;
case "(":
returnCode.push("-·--·");
break;
case ")":
returnCode.push("-·--·-");
break;
case "&":
returnCode.push("·-···");
break;
case ":":
returnCode.push("---···");
break;
case ";":
returnCode.push("-·-·-·");
break;
case "=":
returnCode.push("-···-");
break;
case "-":
returnCode.push("..--.-");
break;
case "_":
returnCode.push("··--·-");
break;
case "$":
returnCode.push("···-··-");
break;
case "@":
returnCode.push("·--·-·");
break;
default:
returnCode.push("?");
break;
}
});

for (let i = 0; i < returnCode.length; i++) {
if (i === returnCode.length - 1) result = `${result}${returnCode[i]}`;
else result = `${result}${returnCode[i]} `;
}

return result;
}

console.log(morseEncode("Código Morse"));
// retorna -.-. --- -.. .. --. --- ..--.- -- --- .-. ... .

console.log(morseEncode("The quick brown fox jumps over the lazy dog"));
// retorna - .... . ..--.- --.- ..- .. -.-. -.- ..--.- -... .-. --- .-- -. ..--.- ..-. --- -..- ..--.- .--- ..- -- .--. ... ..--.- --- ...- . .-. ..--.- - .... . ..--.- .-.. .- --.. -.-- ..--.- -.. --- --.