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.

Hi,

Intend of if(cStr == true) use if(cStr !== -1) on line 12 in your original post. You should get what you wanted.

But your script could be made a lot better than what you have.
For example, you have to reload the page again and again to compare two strings. And that by pressing either F5 or refresh button on the broswer.
You could make that better by using the same button do all that for you.
Moreso, If I were you I would used regular expression which will take care of a lot of parsing of your text.

Hope this helps.

  1. Function "indexOf()" returns number not boolean. If not found "-1" else position "0" or higher
  2. Search string is argument: "string".indexOf("ring") return "2", but "ring".indexOf("string") return "-1"

There were a few things you could clean up. I did it for you, it should work here.

<!doctype html>
    <html>
        <head>
            <meta charset = "utf-8">
            <script type = "text/javascript">
                var str1; //I made this global
                var str2; //I also made this global
                //var cStr = str1.indexOf(str2)>-1;
                function compareStr() //no need to have variables here, you can assign them a value in the function
                    {
                        str1 = prompt(" Please enter some text. ");
                        str2 = prompt(" Please enter some other text. ");
                        var cStr = str1.indexOf(str2);
                        if(cStr > 0) //indexOf() function returns a number, not a boolean. I changed this to > 0, to check if it is there
                            {
                                alert(" Your first set of text was found in your second set of text at index position " + cStr);
                                /* added a parenthesis here to close it,
                                otherwise the whole thing wouldn't work,
                                and changed it to cString, so you don't
                                have to do the calculation again */
                            }
                        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();"> <!--this is called wrong, you aren't assigning a value to str1 and str2. This can be fixed by assigning a value IN the function. --> 
    </html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.