Skip to content

Commit 331a4d2

Browse files
authored
feat: Ciphers/MorseCode Algorithm (TheAlgorithms#1315)
* [feat] New algorithm * [test] Add new test for ParityOutlier.js * [fix] Reset indentation * [fix] Reset indentation * [fix] Style changes * fix: improve code efficiency and a glitch * test: adds a new possible test case * fix: style fix * fix: delete redundant comments and else statements * [fix] style fix * feat: New algorithm * fix: fixed custom code symbols * test: add test for MorseCode * test: add case with custom code symbols * delete files from main branch * fix: style fix * fix: style fix * fix: delete unnecessary quotes
1 parent e6df6eb commit 331a4d2

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

Ciphers/MorseCode.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @author mrmagic2020
3+
* @description Enciphers a combination of letters, numbers and symbols into morse code.
4+
* @see https://en.wikipedia.org/wiki/Morse_code
5+
* @param {string} msg The message to be enciphered.
6+
* @param {string} dot Symbol representing the dots.
7+
* @param {string} dash Symbol representing the dash.
8+
* @returns {string} Enciphered morse code.
9+
* @example morse('Hello World!') = '**** * *-** *-** --- *-- --- *-* *-** -** -*-*--'
10+
*/
11+
const morse = (msg, dot = '*', dash = '-') => {
12+
const key = {
13+
A: '*-',
14+
B: '-***',
15+
C: '-*-*',
16+
D: '-**',
17+
E: '*',
18+
F: '**-*',
19+
G: '--*',
20+
H: '****',
21+
I: '**',
22+
J: '*---',
23+
K: '-*-',
24+
L: '*-**',
25+
M: '--',
26+
N: '-*',
27+
O: '---',
28+
P: '*--*',
29+
Q: '--*-',
30+
R: '*-*',
31+
S: '***',
32+
T: '-',
33+
U: '**-',
34+
V: '***-',
35+
W: '*--',
36+
X: '-**-',
37+
Y: '-*--',
38+
Z: '--**',
39+
1: '*----',
40+
2: '**---',
41+
3: '***--',
42+
4: '****-',
43+
5: '*****',
44+
6: '-****',
45+
7: '--***',
46+
8: '---**',
47+
9: '----*',
48+
0: '-----',
49+
'.': '*-*-*-',
50+
',': '--**--',
51+
'?': '**--**',
52+
'!': '-*-*--',
53+
'\'': '*----*',
54+
'"': '*-**-*',
55+
'(': '-*--*',
56+
')': '-*--*-',
57+
'&': '*-***',
58+
':': '---***',
59+
';': '-*-*-*',
60+
'/': '-**-*',
61+
_: '**--*-',
62+
'=': '-***-',
63+
'+': '*-*-*',
64+
'-': '-****-',
65+
$: '***-**-',
66+
'@': '*--*-*'
67+
}
68+
69+
let newMsg = ''
70+
71+
msg.toString().split('').forEach((e) => {
72+
if (/[a-zA-Z]/.test(e)) {
73+
newMsg += key[e.toUpperCase()].replaceAll('*', dot).replaceAll('-', dash)
74+
} else if (Object.keys(key).includes(e)) {
75+
newMsg += key[e].replaceAll('*', dot).replaceAll('-', dash)
76+
} else {
77+
newMsg += e
78+
}
79+
newMsg += ' '
80+
})
81+
82+
return newMsg.trim()
83+
}
84+
85+
export { morse }

Ciphers/test/MorseCode.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { morse } from '../MorseCode'
2+
3+
describe('Testing morse function', () => {
4+
it('should return an enciphered string with a given input string', () => {
5+
expect(morse('Hello World!')).toBe('**** * *-** *-** --- *-- --- *-* *-** -** -*-*--')
6+
expect(morse('1+1=2')).toBe('*---- *-*-* *---- -***- **---')
7+
})
8+
9+
it('should leave symbols that does not have its corresponding morse representation', () => {
10+
expect(morse('© 2023 GitHub, Inc.')).toBe('© **--- ----- **--- ***-- --* ** - **** **- -*** --**-- ** -* -*-* *-*-*-')
11+
})
12+
13+
it('should be able to accept custom morse code symbols', () => {
14+
expect(morse('Nodejs', '.', '|')).toBe('|. ||| |.. . .||| ...')
15+
})
16+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy