Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  72] [ 4]  / answers: 1 / hits: 21925  / 8 Years ago, fri, april 15, 2016, 12:00:00

I am making a javascript function that tell the user whether the letter they entered is a vowel or consonant. I have tried to make a function but without success. Please someone show whats going on and how to fix it.



Many Thanks



James





function letter() {

var
let = prompt(Your Letter);

if (let == 'a' ||
let == 'e' ||
let == 'i' ||
let == 'o' ||
let == 'u') {

alert(Your letter is a vowel);

} else {

alert(Your letter is a consonant);
}

<!DOCTYPE html>
<html>

<head>

<script>
function letter() {

var
let = prompt(Your Letter);

if (let == 'a' ||
let == 'e' ||
let == 'i' ||
let == 'o' ||
let == 'u') {

alert(Your letter is a vowel);

} else {

alert(Your letter is a consonant);
}​
</script>

</head>
<h3>Vowel or Consonant</h3>
<input id=prompt type=text />
<p id=answer></p>
<button onclick=letter()>Check letter</button>

</body>

</html>




More From » javascript

 Answers
4

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 use letter instead. JS engines may find their way out of your let, but it's bad practice anyway. Besides, since you renamed let to letter, you may want to rename your function to something that actually means what it does, such as displayPromptLetterType.

  • 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) {
...

[#62539] Wednesday, April 13, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;