Created
February 13, 2019 06:17
-
-
Save shalomsam/7ca92ab2c20bad4ce33faa538e9d1adf to your computer and use it in GitHub Desktop.
Given 3 morse code characters determines the letter associated with it. Any of the morse can be substituted for ? and this code will respond with possibilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function possibilities (signals) { | |
var atoms = { | |
".": "E", | |
"-": "T", | |
"I": { | |
".": "S", | |
"-": "U" | |
}, | |
"A": { | |
".": "R", | |
"-": "W" | |
}, | |
"N": { | |
".": "D", | |
"-": "K" | |
}, | |
"M": { | |
".": "G", | |
"-": "O" | |
}, | |
"E": { | |
".": "I", | |
"-": "A" | |
}, | |
"T": { | |
".": "N", | |
"-": "M" | |
} | |
}; | |
var chain = []; | |
var tmp; | |
signals = signals.split(''); | |
for (var i in signals) { | |
var s = signals[i]; | |
if (i == 0) { | |
if (s === '?') { | |
chain.push(atoms['.']); | |
chain.push(atoms['-']) | |
} else { | |
chain.push(atoms[s]) | |
} | |
} else { | |
tmp = chain.reduce((acc, l) => { | |
if (s === '?') { | |
acc.push(atoms[l]['-']) | |
acc.push(atoms[l]['.']) | |
} else { | |
acc.push(atoms[l][s]); | |
} | |
return acc; | |
}, []) | |
chain = tmp; | |
} | |
} | |
return chain; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment