Javascript check if character is a vowel -
i've looked @ plenty of questions related mine here they're using different methods method have use. i'm aware it's long winded way find out when there simpler ways i'm following instructions.
why doesn't below code work? function checks if it's vowel. input checked see if it's length 1. if it's 1, call function. if it's greater 1, ask input until length 1.
i see boolean doesn't exist in js. guess question invalid now!
function isvowel(x){ boolean result; if(x == "a" || x == "e" || x == "i" || x == "o" || x == "u" ) { result = true; } else{ result = false; } return result; } var input; input = prompt("enter character "); input = input.touppercase(); if(input.length == 1){ isvowel(input); } } else{ while(input.length != 1){ prompt("enter character "); if(input.length == 1){ isvowel(input); } } } alert(isvowel(input));
you're calling isvowel in 3 places, , throwing away return value in first two. if want see return value in first 2 places, show (via alert in last example, or of several other ways).
there other issues well:
as devqon points out, you've used
booleanrathervar, code won't parseany time find writing:
var result; if (condition) { result = true; } else { result = false; } return result;...stop , make it:
var result = condition; return result;so
isvowel:function isvowel(x) { var result; result = x == "a" || x == "e" || x == "i" || x == "o" || x == "u"; return result; }(you can, of course, make one-liner, it's easier debug way.)
you have
}afterifblock (reasonable, consistent formatting have make obvious)your
whileloop never end, because never updateinputreturn value ofpromptrather
iffollowedwhile, usedo-while
here's updated version changes
function isvowel(x) { var result; result = x == "a" || x == "e" || x == "i" || x == "o" || x == "u"; return result; } var input; { input = prompt("enter character "); if (input.length == 1) { alert(isvowel(input)); } } while (input.length != 1);
Comments
Post a Comment