function convertToMorseCode(){
let input = document.getElementById(‘english_text’).value;
let output = morseCodeAlphabet(input);
document.getElementById(‘morse_code’).value = output;
}
morseCodeAlphabet(‘hi’);
// this function takes a string input and turns it into morse code and displays
// it on the console
function morseCodeAlphabet(input){
//defines a variable length to find out how long the sting input is
// we are doing this because we need to know every letter in the string
let length = input.length;
//we created these two arrays to store all of the alphabet and
//it’s corosponding morse code
let alpabet = [ ‘ ‘,’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’, ‘,’,’0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′];
let morseCode = [ ‘/’,’.- ‘, ‘.— ‘, ‘-.-. ‘, ‘-.. ‘,
‘. ‘, ‘..-. ‘, ‘–. ‘, ‘…. ‘, ‘.. ‘,
‘.— ‘, ‘-.- ‘, ‘.-.. ‘,’— ‘,’-. ‘,’— ‘,’.–. ‘,’–.– ‘,’.-. ‘,’… ‘,’- ‘,’..- ‘,‘…- ‘, ‘.– ‘,’-..- ‘,
‘-.– ‘,’–.. ‘,’, ‘,’—-– ‘,’.—-‘,’..— ‘,’…– ‘,‘….- ‘,’…..’,’-….’,’–... ‘,’—.. ‘,’—-. ‘];
console.log(‘alphabet array length’,alpabet.length);
console.log(‘morse code array length’, morseCode.length);
//this variable is used for displaying the morse code onto the console in one line
let result = ”;
//this for loop takes the first letter in the string input and converts it into morse code
//and repeats it untill the end of the input
for(i = 0; i< length; i++){
//this variable finds the first letter in the string and repeats for every itteration
let carName = input.substring( i ,i+1);
//console.log(carName);
//this variable finds the position of carname in the alphabet array and assigns it to idx
let idx = alpabet.indexOf(carName);
//this variable finds the conrosponding morse code base
//on possition from the mose code array
let mcode = morseCode[idx];
//this line adds mcode to the previous morse code result
result = result + ‘ ‘ + mcode;
}
//this console.log logs the final result
console.log(result);
return result;
}