+ 1
How to do it so if the prompt contains "a" do something?
10 Answers
+ 3
You can use includes() like this
function morse() {
if (words.includes("a")) {
code=code+".- ";
};
if (words.includes("b") ){
code=code+"-... ";
};
alert(code);
}
edit:
"abcdefghijk".includes(words)
like this?
+ 2
Yes but I want it to be if you put in the prompt ab so it will be .- -... (morse code). But it won't show anything.
+ 2
Lego in Motion
Why to use loop and includes function when you can use replaceAll function.
words = words.replaceAll("a", ".- ").
replaceAll("b","-... ").
replaceAll("c", "-.-. ");
alert(words);
+ 1
What is test("b") there?
+ 1
Oops! Now it's good!
+ 1
You can remove test()
https://code.sololearn.com/WCDYRRKLbr4k/?ref=app
+ 1
Check the code again, I added an if statement
+ 1
Oh yes thanks. But if I want to do the whole alfabet and any letters together I would have to do literally bilions of if statement. Thanks for trying to help though.
0
Are you trying to convert an input string to morse code?
If yes....just do a "for of" loop on the string.
I'm sure you can use a "switch case" construct on strings in JavaScript, might be a bit tidier.
0
Hello