Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js decoding morse code

For this project, I am trying to decode a given Morse code string. Encoded characters are separated by a single space and words are separated by three spaces. I am having a tough time getting past the word spaces. I keep getting "wordundefinedword".

decodeMorse = function(morseCode) {
    outPut = "";

    for (var i = 0; i < morseCode.split(" ").length; i++) {
        if (i === "   ") {
            outPut += " ";
        } else {
            outPut += MORSE_CODE[morseCode.split(" ")[i]];
        }

    }
    return outPut;
}

Example: "".... . -.--" "-- .- -."" -> "HEY MAN" Sorry for the weird quotes. It wouldn't show the spaces without the outer ones.

like image 284
Allissia Baublet Avatar asked Sep 17 '25 16:09

Allissia Baublet


2 Answers

Here is a method that uses .map(), .split(), and .join().

function decodeMorse(morseCode) {
  var ref = { 
    '.-':     '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',
    '.----':  '1',
    '..---':  '2',
    '...--':  '3',
    '....-':  '4',
    '.....':  '5',
    '-....':  '6',
    '--...':  '7',
    '---..':  '8',
    '----.':  '9',
    '-----':  '0',
  };

  return morseCode
    .split('   ')
    .map(
      a => a
        .split(' ')
        .map(
          b => ref[b]
        ).join('')
    ).join(' ');
}

var decoded = decodeMorse(".-- --- .-. -..   .-- --- .-. -..");
console.log(decoded);
like image 151
Joseph Marikle Avatar answered Sep 19 '25 06:09

Joseph Marikle


Maybe have two loops nested instead. The outside loop splits your morse code by three spaces, and the inner loop splits the word by one space. That will parse out the letter, then you can map the alpha-numeric character by using an enum of the morse code letters.

// message = Halp! Morse code is driving me nuts!
var message = ".... .- .-.. .--. -·-·--     -- --- .-. ... .     -.-. --- -.. .     .. ...     -.. .-. .. ...- .. -. --.     -- .     -. ..- - ... -·-·--"; 
var alphabet = {  
   "-----":"0",
   ".----":"1",
   "..---":"2",
   "...--":"3",
   "....-":"4",
   ".....":"5",
   "-....":"6",
   "--...":"7",
   "---..":"8",
   "----.":"9",
   ".-":"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",
   "/":" ",
   "-.-.--":"!",
   ".-.-.-":".",
   "--..--":","
};
var messageConverted = [];

message.split("   ").map(function (word) {
    word.split(" ").map(function (letter) {
        messageConverted.push(alphabet[letter]);
    });
    messageConverted.push(" ");
});
 
console.log(messageConverted.join(""));

Or something like that. That enum is not complete (caps, punctuation), but you get the idea.

like image 20
EricL Avatar answered Sep 19 '25 05:09

EricL