Using JavaScript to encode morsecode
javascript
Solution
First of all, you have to strip off all the characters you can't encode:
phrase = phrase.toLowerCase().replace(/[^a-z]/g, "");
Using `replace` and a regular expression, you'll end up with a string of only alphabetic characters. We also convert all the letter to lowercase for semplicity.
Then, inside the for loop:
c = phrase.charCodeAt(i);
That would convert the letter into its equivalent ASCII code value. The corrisponding morse code would then be `morseCode[c - 97]`.
As Gerald Schneider suggested, you can improve this encoding with numbers too, but the code would be a bit more complex.
Problem
I'm trying to convert any text string into Morse Code in the simplest way possible. I am very new to programming so please can you give me some advice on what methods i could use. I have so far just written a phrase(string) and an Array holding the Morse Code but i am struggling on what steps to use next on how to take each character of the string then checking it with the array and printing out the Morse Code equivalent of the string. ``` var phrase = "go down like a lead balloon"; var morseCode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."] for(i=0; i<phrase.length; i++){ c = phrase.charAt(i); WScript.echo(c + " | " + i); } ```