0

I really lost and don't know what to do. I tried to link the html form in put to a java-script function.

but the problem is I do not know my steps are correct or not. the other thing is how I can show the output on the page.

function fnCountVowels(string) {
  var getinput = document.getElementsbyid("Vowel1");
  console.log(Vowel1.value);

  for (i = 0 < getinput.length; i++) {
    chr = getinput.charat(i);

    if (Chr == "a" || (chr == "e") || (chr == "i") || (chr == "o") || (chr == "u")) {
      getinput = getinput + 1;
    }
  }

  return getinput;
}
<div id="Vowel" class="Tab">
  <form action=""  id="form" onsubmit="fnCountVowels(string)">
    Enter String: <br>
    <input id="Vowel1" type="text" name="Enter the string "><br><br>
    <button type="submit" onclick"fnCountVowels(string)">Click</button>
  </form>

2
  • 1
    Please lay out your code so that it is readable. Remove the redundant blank lines and make the indents sensible. Commented Nov 16, 2018 at 12:53
  • Is there a specific question here beyond “My code doesn’t work, please help”? Commented Nov 16, 2018 at 13:03

2 Answers 2

-1

Here is a solution based on your code, but simplifying and "modernizing" it a bit:

function countVowels() {
  var text = document.forms["CountVowels"].text.value,
  nmrOfVowels = 0;
  console.log( text);
  for (let i=0; i < text.length; i++) {
let char = text.charAt(i);
if ("aeiou".includes( char)) {
  nmrOfVowels = nmrOfVowels + 1;
}
  }
  console.log("Number of vowels: ", nmrOfVowels);
  document.getElementById("showResult").textContent = "Number of vowels: "+ nmrOfVowels;
}
<form id="CountVowels">	
  <label>Enter text: <input name="text"/></label>
  <button type="button" onclick="countVowels()">Count vowels</button>
</form>
<p id="showResult"></p>

Sign up to request clarification or add additional context in comments.

Comments

-1

Try this below,

function fnCountVowels()
{
  var vowelCount = 0;
  var vowels = [];
  var getinput = document.getElementById("Vowel1").value;
 
  for (var i = 0; i < getinput.length; i++) 
  {
	  chr = getinput.charAt(i);
    
    if (chr == "a" || (chr == "e") || (chr == "i") || (chr == "o") || (chr == "u"))
    {
        vowelCount += 1;
        vowels.push(chr);
    }
  }

console.log("Input: " + getinput);
console.log("Vowels Count: " + vowelCount);
console.log("Vowels: " + vowels);
}
<div id="Vowel" class="Tab">

<form>
	
Enter String: <br>
<input id="Vowel1" type="text" name="Enter the string "> <br><br>

	<button type="button" onclick="fnCountVowels()"> Click </button>
	

</form>

</div>

3 Comments

Notice that using varin a code block (like in for (var i = 0;...) is a code smell since it suggests that a block-scope variable is declared, which is not the case!
For adding an element to the end of an array, we use push. Consequently, you should code vowels.push( chr)!
Agree! Just added it the basic way for understanding.. However, modified now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.