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:
Output 1: Success case
Output 2: When the input string contains a white space
Output 3: When there is no vowel present
How to execute this script?
- Copy the code to a text file and save it with a .html extension.
- Open this file in a browser.
- You will be asked to enter a string and the result will be displayed.
- 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.