list - If variable is equal to value or another value - javascript -
i'm trying write simple pig latin code. want check if first letter of input string vowel, if run code. how can ask if y equal 1 of values.
function piglatin() { var y = prompt("enter word:") if (y.charat(0) = a,e,i,o,u) { var piglatin = y + "ay"; document.getelementbyid("answer").innerhtml = "answer is: " + piglatin; } else { var wordlength = y.length + 1; var piglatin = y.substring(1, wordlength) + y.charat(0) + "ay"; document.getelementbyid("answer").innerhtml = "answer is: " + piglatin; } }
if (y.charat(0) = a,e,i,o,u) {
...is not valid.
try...
if ('aeiou'.includes(y.charat(0))) {
your problems are...
- strings must quoted, if they're 1 character long.
- an array must wrapped
[
,]
(unless constructed via means sucharray
constructor) - you can't use assignment operator (
=
), nor equivalency operator (==
or===
) automatically check presence of item in array. since you're checking against list of single characters, can directly usestring.prototype.includes()
on it.
if user cancels prompt()
, null
also, not handling (it crash on condition mentioned).
Comments
Post a Comment