Vowels !

+3 James Moore · November 25, 2014
<html>
<body>
<script "text/JavaScript">
var a = prompt(" Please enter a Vowel! ");

function letter(a){
if(a == "a" || a == "e" || a == "i" || a == "o" || a == "u"){
alert(a + "  congrats this is a vowel!! ");
}else{
alert("Sorry try again " + a + " is not a vowel!" );
}
}

letter(a);

</script>

</body>

okay guys heres my code it works(open to suggestions) but i want to make it so my prompt only allows the user to type in 1 number and how to allow capital letters as well without adding || a = "A" || a ="I" and so on 

Post a Reply

Replies

Oldest  Newest  Rating
0 Landon Luman · November 26, 2014
I don't know how to limit the characters, but what you can do is something like this!

You can split the characters, then get the first character!
<html>
<body>
<script "text/JavaScript">
var a = prompt(" Please enter a Vowel! ");

function letter(a){
a = a.split('');
if(a[0] == "a" || a[0] == "e" || a[0] == "i" || a[0] == "o" || a[0] == "u"){
alert(a + " congrats this is a vowel!! ");
}else{
alert("Sorry try again " + a + " is not a vowel!" );
}
}

letter(a);

</script>

</body>

This should work

-Landon
0 James Moore · November 28, 2014
Never seen the .split before but thats great ! wonderful idea man thanks :P :)
+1 Samuel Oloruntoba · November 29, 2014
Here my suggestion
String.prototype.firstLetterIsVowel = function() {
  var vowels = ['a', 'e', 'i', 'o', 'u'],
      char   = this[0];
  
  return vowels.indexOf(char.toLowerCase()) >= 0;
};

and to use it you would just simply call the function on a string like this
"forum".firstLetterIsVowel(); // this would return false

for a simple example, you would do something like this
var letter = window.prompt("Enter a sentence");

if( letter.firstLetterIsVowel() ) {
alert('good');
} else {
alert('no good');
}

More examples include
"Arrow".firstLetterIsVowel(); // returns true
"Zip".firstLetterIsVowel(); // returns false

"a".firstLetterIsVowel() // returns true
0 Landon Luman · December 3, 2014
Man, we're all learning a little bit of javascript from samuel :D
0 Sochima Biereagu · January 10, 2015
var a = prompt(" Please enter a Vowel! ");
function letter(a)
{
if(a.toString().match(/^[aeiou]/gi)){
alert(a + " congrats this is a vowel!! ");
}else{
alert("Sorry try again " + a + " is not a
vowel!" );
}
}

letter("Apple"); //"Apple congrats this is a vowel!! "
  • 1

Javascript

107,076 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators