So I'm trying to make a script that prompts for two strings, then searches the second string to see if it contains the first using a function triggered by a button. Something is off with my comparison. Let me know what I am doing wrong.
<!doctype html>
<html>
<head>
<meta charset = "utf-8">
<script type = "text/javascript">
var str1 = prompt(" Please enter some text. ");
var str2 = prompt(" Please enter some other text. ");
//var cStr = str1.indexOf(str2)>-1;
function compareStr(str1,str2)
{
var cStr = str1.indexOf(str2);
if(cStr == true)
{
alert(" Your first set of text was found in your second set of text at index position " + str1.indexOf(str2))+1;
}
else
{
alert(" Your first set of text is not contained in your second set of text. ");
}
};
</script>
</head>
<input type = "button" value = "Compare strings" onclick = "compareStr(str1,str2)";>
</html>
I am thinking that my problem lies somewhere in my "indexOf" method code, but I cannot figure out what it is.