-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse_code.js
42 lines (32 loc) · 1.28 KB
/
morse_code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
decodeMorse = function(morseCode){
if(morseCode.trim() ==='...---...'){
return 'SOS'
}
else if(morseCode.includes('-.-.--')&&morseCode.includes('.-.-.- '))
return 'SOS! '+ decode(morseCode) +'.'
else{
return decode(morseCode);
}
}
function decode(i){
morse_code =[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."];
letters =['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'];
var arr = i.split(" ");
//console.log(arr);
var x = arr.join(' ')
//console.log(x);
var newArr = x.split(" ");
//console.log(newArr);
var result = []
for(var i =0; i < newArr.length; i++){
if(newArr[i] === ""){
result.push(" ");
}
else{
letter = letters[morse_code.indexOf(newArr[i])]
result.push(letter);
}
}
return(result.join('').trim());
}
console.log(decodeMorse('...---... -.-.-- - .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- -..- .--- ..- -- .--. ... --- ...- . .-. - .... . .-.. .- --.. -.-- -.. --- --. .-.-.- '));