Skip to content

JavaScript to Find the Position of Left Most Vowel

This program includes Javascript to find the position of the leftmost vowel in a string. This javascript is quite simple. We run a loop until the length of the input string is reached. Inside the loop, we go through each character of the string and check if a vowel (a, e, i, o, u) is present.

Input Parameter: A string
Output:
Left-most vowel and the position of the left most vowel in the string

FindLeftmostVowel.html

<!DOCTYPE HTML>
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<script type ="text/javascript">
function str_vowel()
{
	var str=prompt("Enter the string\n", " ");
	for(var i = 0; i<str.length; i++)
	{
		if (str.charAt(i) =='a' || str.charAt(i) == 'e' || str.charAt(i) =='i'
		|| str.charAt(i) == 'o' || str.charAt(i) == 'u' || str.charAt(i) == 'A' ||
		str.charAt(i) == 'E' || str.charAt(i) =='I' || str.charAt(i) =='O' || str.charAt(i) == 'U')
		{
			document.write("The entered string is:" +str+ "<br/>");
			document.write("The leftmost vowel is :"+str.charAt(i)+"<br/>");
			var pos = i+1;
			document.write("The position of the leftmost vowel " +str.charAt(i)+ " is:" +pos+"\n");
			exit;
		}
	}
	document.write("The entered string is:" +str+ "<br/>");
	document.write("The entered string has no vowels");
}
</script>
</head>
<body style="background-color:green" onload = "str_vowel();"> <!-- Not recommended to use insline styeling -->
</body>
</html>

Other Programs:

Go through the following video to understand how to run this javascript:

 

Input:

JavaScript to find the position of the left most vowel in a string

Output 1: Success case

Output of the left most vowel in a given string using javascript
diagram showing the left most vowel in the given string using javascript

Output 2: When the input string contains a white space

Output of the left most vowel in the given string with space using javascript
The diagram shows the left most vowel in a string when there is a white space

Output 3: When there is no vowel present

javascript to find left most vowel in a string output

How to execute this script?

  1. Copy the code to a text file and save it with a .html extension.
  2. Open this file in a  browser.
  3. You will be asked to enter a string and the result will be displayed.
  4. To check for another string, refresh the web page and enter a new string. You can refer to the video for easy understanding.

Please let me know if you come across any issues.

Did it help? Would you like to express?