Hi,
In a for loop I am trying repeatedly to search for a given string ("motto") in the string
variable "str". But it never finds a match. But, as far as the logic is concerned it should find "motto" in variable str three times and print it to the screen.
Current output: Nothing is displayed, it doesnt even enter the if loop
Expected output: motto motto motto
The code seems to be correct, but it does not display the ouput in the way it sholud be.
Below is the complete code, can be directly copied to notepad and open in a browser.
Code:
<script language="JavaScript" type="text/javascript">
function T()
{
var key = "motto";
var str = "hello motto test";
var res = " ";
for(var i=0; i<3;i++)
{
alert("in for");
if(str.indexof(key) != -1)
{
alert("inside if");
res += key + " ";
}
else
alert("no match");
}
alert(res);
}
</script>
<form name="search" >
<div align="center">
<input type="submit" onclick="T()"> </div>
</form>
It looks simple, but I think I am missing some thing any help?.
Thanks,