Javascript check if character is a vowel
You're calling isVowel
in three places, and just throwing away the return value in the first two. If you want to see the return value in the first two places, show it (via alert
as in your last example, or any of several other ways).
There are other issues as well:
As devqon points out, you've used
boolean
rather thanvar
, so the code won't parseAny time you find yourself writing:
var result;
if (condition) {
result = true;
} else {
result = false;
}
return result;...stop and make it:
var result = condition;
return result;So for
isVowel
:function isVowel(x) {
var result;
result = x == "A" || x == "E" || x == "I" || x == "O" || x == "U";
return result;
}(You can, of course, make that a one-liner, but it's easier to debug this way.)
You have an extra
}
after yourif
block (reasonable, consistent formatting would have make that obvious)Your
while
loop will never end, because you never updateinput
with the return value of theprompt
Rather than an
if
followed by awhile
, usedo-while
Here's an updated version with just those changes
function isVowel(x) {
var result;
result = x == "A" || x == "E" || x == "I" || x == "O" || x == "U"; return result;}
var input;
do { input = prompt("Enter a character "); if (input.length == 1) { alert(isVowel(input)); }} while (input.length != 1);
Checking a character is a vowel
Because once you've found a match, you need to break from the loop and exit the function.
Correct way to handle false:
function isItAVowel(letter) {
var vowel = ["a", "e", "i", "o", "u"];
for (var i=0; i < vowel.length; i++) {
if (letter == vowel[i]) {
document.getElementById("paragraph").innerHTML = "vowel";
return;
}
}
document.getElementById("paragraph").innerHTML = "not vowel";
}
isItAVowel("i");
Since everyone is posting fun ways of doing this differently, here is a way to do it without a function at all, with the fastest execution:
var isItAVowel = {
'a': true,
'e': true,
'i': true,
'o': true,
'u': true };
!!isItAVowel["a"] == true;
!!isItAVowel["o"] == true;
!!isItAVowel["c"] == false;
Check if is vowel or consonant
To tweak your current code, you can have a variable which indicates whether the character is a consonant, and starts out as true
, and if you find a vowel, reassign it to false
, then check that variable:
function letter(arg) { var vowel = "aeouiAEOUI"; var result = ""; let isConsonant = true; for (var i = 0; i < vowel.length; i++) { if (arg === vowel[i]) { isConsonant = false; } } if (isConsonant) { console.log("consonant"); } else { console.log("vowel"); }}
letter('a');letter('f');
How to find if the first letter of a string is a vowel in javascript
You're quite close, just slice the word using [0]
and check that way:
function startsWithVowel(word){ var vowels = ("aeiouAEIOU"); return vowels.indexOf(word[0]) !== -1;}
console.log("apple ".concat(startsWithVowel("apple") ? "starts with a vowel" : "does not start with a vowel"));console.log("banana ".concat(startsWithVowel("banana") ? "starts with a vowel" : "does not start with a vowel"));
How to check if a string character is in a JavaScript array?
Is it this what you're looking for?
function checkVowel(str) {
const counts = Object.seal({ a: 0, e: 0, i: 0, o: 0, u: 0 });
for (let char of str) {
counts[char.toLowerCase()]++;
}
return counts;
}
console.log(checkVowel("hello world"));
JavaScript: vowell or consonant function
Open the dev tools and see what the console tells you.
I see three mistakes :
- you're missing a closing
}
bracket at the end of the function. - you're using the reserved word
let
as a variable name. Say you useletter
instead. JS engines may find their way out of yourlet
, but it's bad practice anyway. Besides, since you renamedlet
toletter
, you may want to rename your function to something that actually means what it does, such asdisplayPromptLetterType
. - The way to retrieve the user's input from the field near the button is
var letter = document.getElementById("prompt").value;
Besides, rather than a long if
condition, you may want it a tad more data driven :
var vowels = ["a","e","i","o","u"];
if (vowels.indexOf(letter.toLowerCase()) >= 0) {
...
Related Topics
Reload Datatable After Ajax Success
How to Access a Model Attribute With JavaScript Variable
How to Get Pasted Value from Reactjs Onpaste Event
Bootstrap Datepicker Set Default Date Not Working
Webaudio - Seamlessly Playing Sequence of Audio Chunks
Angular 4 - Cannot Read Property Status of Null While Displaying Validation Error
Error: Permission Denied to Access Property "Document"
Expanding Only One Row When Button Clicked
Finding Sequential Values in Js Array
Reactjs Dynamic Drop Down Populated With Map Function
Javascript Loop Wait Until API Call Finished
I Want to Change the Page Background Color, But It Doesn't Work as I Expect in Chrome
Splitting an Array of Objects by Property
Get an Attribute from the Clicked Element With JavaScript
Slick.Js: Get Current and Total Slides (Ie. 3/5)
How to Separate an Integer into Separate Digits in an Array in JavaScript