Here is a script which prints the position of the character in a given string. Here using charAt() built in function to traverse the characters.
Enter the string here :
Enter the character to find :
<!DOCTYPE html> <html> <body> <lable>Enter the string here : </lable> <input type="text" name="enter" id="text" value=""/> <br/> <lable>Enter the character to find : </lable> <input type="text" name="enter" id="char1" value=""/> <br/> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var str = document.getElementById('text').value; var len = str.length; var mychar = document.getElementById('char1').value; var k=0; for(var i=0; i<len;i++) { search = str.charAt(i); if (search == mychar) { alert("Found at position = " + i); } } } </script> </body> </html>
Try Output Here :
No comments:
Write comments