Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions projects/m2/019-morse-code/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const readline = require('readline/promises');
const { stdin: input, stdout: output } = require('process');


const morse_code_dict = {
'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': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----',
'.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--', '/': '-..-.',
'(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...', ';': '-.-.-.', '=': '-...-',
'+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.', '$': '...-..-', '@': '.--.-.',
' ': '/'
}



function getMorse(string){

let result='';
for(let index = 0 ; index < string.length;index++){
let add ='';
if(index < string.length - 1){
add =' '
}
const char = string[index];

const morseChar= morse_code_dict[char.toUpperCase()]??''
result= result + morseChar + add;
}
return result;

}



async function main() {


const rl = readline.createInterface({ input, output });
try{
const stringInput=await getInput(rl);
const inputMorse = getMorse(stringInput)
console.log(inputMorse);
console.log('.... . .-.. .-.. --- .-- --- .-. .-.. -..');



}
catch(e){
console.log(e)
}
finally{
rl.close();
}

}
main();




async function getInput(rl){

const input = await rl.question(`Please enter the string: `)
//const inputNumber = Number.parseInt(input,10);
if(input.trim().length > 0){
return input;
}

console.log("Please Enter a valid string")
return getInput(rl);

}