I am struggling to pass the below test, passing in the following following MorseCode to my code, "--. --- --- -.. -- --- .-. -. .. -. --. -. --- .-. - .... -.-. --- -.. . .-. ..." Any ideas on how I can handle " " 3 spaces without affecting the translation? Expected: "GOOD MORNING NORTHCODERS" Received: "GOODMORNINGNORTHCODERS"
function morseCode(str) {
if (str === undefined) {
return "";
}
const morseDic = {
".-": "A",
"-...": "B",
"-.-.": "C",
"-..": "D",
".": "E",
"..-.": "F",
"--.": "G",
"....": "H",
"..": "I",
".---": "J",
"-.-": "K",
".-..": "L",
"--": "M",
"-.": "N",
"---": "O",
".--.": "P",
"--.-": "Q",
".-.": "R",
"...": "S",
"-": "T",
"..-": "U",
"...-": "V",
".--": "W",
"-..-": "X",
"-.--": "Y",
"--..": "Z",
"-----": "0",
".----": "1",
"..---": "2",
"...--": "3",
"....-": "4",
".....": "5",
"-....": "6",
"--...": "7",
"---..": "8",
"----.": "9",
"*-": " "
}
const outArr = [];
const myArr = str.split(' ');
myArr.forEach((el) => outArr.push(morseDic[el]));
let newString = outArr.toString();
newString = newString.replaceAll(",", '');
return newString;
};
const result = morseCode("--. --- --- -.. -- --- .-. -. .. -. --. -. --- .-. - .... -.-. --- -.. . .-. ...");
console.log(result);