0

Where am I going wrong in this code?

    // Eyetee's Third Toy V0.0. Copyright 2012 by Eyetee and not released.    
    // Declare variables needed
    var string_to_be_examined = "This is a test string that can also be obtained from \
    some other location."
    var substring_to_test_for
    var i = 0
    var j = 0
    var storage_string = []
    var location_found = []

    // Obtain substring to test for
    substring_to_test_for = prompt("Type in the text you wish to search for:")

    // Test for presence of substring 
    for (i; i < string_to_be_examined.length; i++)  {
        if (string_to_be_examined[i] = substring_to_test_for[j])   {
            storage_string.push(substring_to_test_for[j])
            if (j < substring_to_test_for.length)   {
                j++
            }
            if (storage_string === string_to_test_for)  {
                location_found.push[j]
                j = 0
            }
        }
    };

    if (location.found = "")    {
        alert("Your text wasn't found!")
    }
    else    {
        for (k = 0; k < location_found.length; k++) {
            alert("Your text was found starting at the " + location_found.string[k] + "th character.")
        }
    }

Thanks to everyone for their help.

Edited by pritaeas: Moved.

2
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by Eyeteeorg
0

This is a revised version of the code, which currently does not produce output I can see. It's supposed to take a variable called string_to_be_examined (which this script provides but can be input from elsewhere), then request user input of string_to_test_for and search string_to_be_examined for occurrences of that latter string, providing either an alert() stating failure to find the latter string or a separate alert() window citing every location the latter string was found within the original string. As I said, this script currently does not print any alerts.

// Eyetee's Third Toy V0.0. Copyright 2012 by Eyetee and not released.    
// Declare variables needed
var string_to_be_examined = "This is a test string that can also be obtained from \
some other location."
var substring_to_test_for
var i = 0
var j = 0
var storage_string = []
var location_found = []

// Obtain substring to test for
substring_to_test_for = prompt("Type in the text you wish to search for:")

// Test for presence of substring 
for (i; i < string_to_be_examined.length; i++)  {
    if (string_to_be_examined[i] === substring_to_test_for[j])   {
        storage_string.push(substring_to_test_for[j])
        if (j < substring_to_test_for.length)   {
            j++
        }
        if (storage_string === substring_to_test_for)  {
            location_found.push[j]
            j = 0
        }
    }
};

if (location.found = "")    {
    alert("Your text wasn't found!")
}
else    {
    for (k = 0; k < location_found.length; k++) {
        alert("Your text was found starting at the " + location_found.string[k] + "th character.")
    }
}

And while we're at it, the following very simple script keeps crashing my scratchpad with the error cited at the bottom:

var stringInput = prompt("Type something in and hit enter:");
var reverseString = "";
for (i = stringInput.length - 1; i >= 0; i--)   {
    reverseString = reverseString + stringInput.substring(i, i + 1);
}
alert("The reverse of what you typed is " + reverseString);
/*
Exception: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMWindow.alert]
@6
*/

All input appreciated.

Edited by Eyeteeorg: corrected typos

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.