Kindly Note that Ignore Issues at the time of program execution. All programs are reference only.
6)Write a JavaScript program to read
a character string from user and perform the following functions:
a.Accept a character from user and
count the number of occurrences of that character in the string.
b.Accept a Position from user and
print the character at specified position. (Use promt() function for accepting
a character)
<html>
<head>
<script>
function count()
{
var
s=document.getElementById("c").value;
if(document.getElementById("c1").checked==true)
{
var ch=prompt("Enter the
Character");
var i;
var cnt=0;
for(i=0;i<s.length;i++)
{ if(s[i]==ch)
{ cnt++; }
}
document.write(cnt);
}
if(document.getElementById("c2").checked==true)
{
var pos=prompt("Enter the
Position:");
document.write(s[pos]);
}
}
</script>
</head>
<body>
<input
type="text"id="c"placeholder="Enter String">
<br>
<input
type="radio"id="c1"name="option">
Count the number of occurrences of a
particular character<br>
<input
type="radio"id="c2"name="option">
Display the character at the
Specified position<br>
<button
onclick="count()">Submit</button>
</body>
</html>