dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.4 KiB
2.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a22d77ddf034bc4e35b1d5c | Challenge 355: Morse Code | 28 | challenge-355 |
--description--
Given a Morse code string, return the decoded message using the following table:
| Code | Letter | Code | Letter |
|---|---|---|---|
.- |
A |
-. |
N |
-... |
B |
--- |
O |
-.-. |
C |
.--. |
P |
-.. |
D |
--.- |
Q |
. |
E |
.-. |
R |
..-. |
F |
... |
S |
--. |
G |
- |
T |
.... |
H |
..- |
U |
.. |
I |
...- |
V |
.--- |
J |
.-- |
W |
-.- |
K |
-..- |
X |
.-.. |
L |
-.-- |
Y |
-- |
M |
--.. |
Z |
- Letters are separated by a single space
- Words are separated by three spaces
--hints--
decodeMorse("--..") should return "Z".
assert.equal(decodeMorse("--.."), "Z");
decodeMorse("... --- ...") should return "SOS".
assert.equal(decodeMorse("... --- ..."), "SOS");
decodeMorse("..-. .-. . . -.-. --- -.. . -.-. .- -- .--.") should return "FREECODECAMP".
assert.equal(decodeMorse("..-. .-. . . -.-. --- -.. . -.-. .- -- .--."), "FREECODECAMP");
decodeMorse(".... . .-.. .-.. --- .-- --- .-. .-.. -..") should return "HELLO WORLD".
assert.equal(decodeMorse(".... . .-.. .-.. --- .-- --- .-. .-.. -.."), "HELLO WORLD");
decodeMorse("- .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- -..- .--- ..- -- .--. . -.. --- ...- . .-. - .... . .-.. .- --.. -.-- -.. --- --.") should return "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG".
assert.equal(decodeMorse("- .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- -..- .--- ..- -- .--. . -.. --- ...- . .-. - .... . .-.. .- --.. -.-- -.. --- --."), "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG");
--seed--
--seed-contents--
function decodeMorse(code) {
return code;
}
--solutions--
function decodeMorse(code) {
const table = {
'.-': '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'
};
return code.split(' ')
.map(word => word.split(' ').map(c => table[c]).join(''))
.join(' ');
}