Using JavaScript to encode morsecode

I am trying to convert any text string to Morse code in the simplest way possible. I am very new to programming, so please can you give me some advice on what methods I can use.

I have so far just written a phrase (string) and an array containing Morse code, but I'm afraid which steps should be taken next, how to take each character of the string and then check it with the array and print the Morse code is equivalent to a string.

var phrase = "go down like a lead balloon";

var morseCode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",     ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]

for(i=0; i<phrase.length; i++){

c = phrase.charAt(i);

WScript.echo(c + " | " + i);
}  

      

+3


source to share


3 answers


First of all, you need to strip all characters that you cannot encode:

phrase = phrase.toLowerCase().replace(/[^a-z]/g, "");

      

Using replace

and regular expression , you get a string of alphabetic characters only. We'll also convert all letters to lowercase for semplicity.

Then inside the for loop:



c = phrase.charCodeAt(i);

      

This converts the letter to its equivalent ASCII value. Corresponding Morse code will be morseCode[c - 97]

.

As Gerald Schneider suggested, you can improve this encoding with numbers too, but the code will be a little more complicated.

+4


source


You can use a dictionary like:



var alphabet = {
    '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': '-----', 
}

"This is a sentence containing numbers: 1 2 3 4 5"
    .split('')            // Transform the string into an array: ['T', 'h', 'i', 's'...
    .map(function(e){     // Replace each character with a morse "letter"
        return alphabet[e.toLowerCase()] || ''; // Lowercase only, ignore unknown characters.
    })
    .join(' ')            // Convert the array back to a string.
    .replace(/ +/g, ' '); // Replace double spaces that may occur when unknow characters were in the source string.

// "- .... .. ... / .. ... / .- / ... . -. - . -. -.-. . / -.-. --- -. - .- .. -. .. -. --. / -. ..- -- -... . .-. ... / .---- / ..--- / ...-- / ....- / ....."

      

+8


source


As mentioned, you can convert an array to an object for one-to-one matching. This way, you don't need to worry about erroneous characters ending up in the output, because the decode function filters them out if they are not present as a key.

var morseObj = {};
for (var i = 97, l = 97 + morseCode.length; i < l; i++) {
  morseObj[String.fromCharCode(i)] = morseCode[i - 97];
}

      

And then execute the decode function:

function encode(sentence) {
  var output = '';
  for (var i = 0, l = sentence.length; i < l; i++) {
    var letter = sentence[i].toLowerCase();
    if (morseObj[letter]) { output += morseObj[letter] + ' '; }
  }
  return output;
}

encode('My name is Andy'); // "-- -.-- -. .- -- . .. ... .- -. -.. -.-- "

      

DEMO

0


source







All Articles