I figured it best to continue here rather than start new thread.
I tested Mac's code above, and great it works. Then I wanted to apply Array methods and I "broke it".
(see Mac's code below) I put the function in a Script Object. I used 2 buttons... one to populate the array elements, and another to test the match. Tried User Text Input for entering "jungle", "frog", etc to myGuess. Did set-up myWords and myGuess as page variables.
Here are my questions and issues.
1) I can only get the code to work if I
change the "if statement" in the function
to:
Code:
if (String.contains(myWords, str, true)) // remove the [i] index
Why is that necessary? Is it right?
Also, I tried leaving the "[i]" in and creating another loop to sequence thru Array Elements. I couldn't get it working. How could this be done?
2) I wanted to prevent User 'null' input for myGuess. I tried to insert an If statement, if(myGuess==''), but mixed results. Not sure that belongs in the function or in the match-test??
Thanks.
Here's a repeat of the code with remarks how I divided it.
Code:
[/ Execute by Button1 script
myWords = "SNAIL,BEE,CRICKET,ANT,FROG,JUNGLE".split(",")
// Have User Text Input to type in jungle instead of the line below
myGuess = "jungle"
// Use Button2 script
if (inArray(myGuess)) Debug.trace("Word found - increase score")
// Place this function code in a Script Object on the page //seems this Obj must be high in the organizer
function inArray(str)
{
for (i=0;i<myWords.length;i++)
{
if (String.contains(myWords[i], str, true)) // does not seem to work within a Script Object
{
return true
} else
{
return false
}
}
}