CodeWars 6kyu. Decode the Morse code

CodeWars 6kyu. Decode the Morse code

CodeWars 6kyu. Decode the Morse code

Decode Morse code to plain text

The Morse code encodes every character as a sequence of “dots” and “dashes”. For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−−. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message HEY JUDE in Morse code is ···· · −·−− ·−−− ··− −·· ·.

NOTE: Extra spaces before or after the code have no meaning and should be ignored.

In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···−−−···. These special codes are treated as single special characters, and usually are transmitted as separate words.

Your task is to implement a function that would take the morse code as input and return a decoded human-readable string.

1
2
decodeMorse('.... . -.--   .--- ..- -.. .')
//should return "HEY JUDE"
  1. Each word distinguished by " " (3 spaces)
  2. Free to use the preloaded Morse code table as a dictionary. By MORSE_CODE['.--']

javaScript Solution

Submit screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
decodeMorse = function(morseCode){
var words = morseCode.split(" ");
var string = ""

for (var i in words){
if(words[i] != ''){
var word = words[i].split(" ");
for(var j in word){
if(word[j] != ''){
string += MORSE_CODE[word[j]];
}
}
if(i < words.length-1){
string += " "
}
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×