Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't find error in simple javascript 1

Status
Not open for further replies.

PulsarSL

Programmer
Jul 4, 2005
24
NO
Hey -- When I have this javascript in my file and try to execute a function defined after it, the function is never executed. When I remove this piece, it works fine. This leads me to believe that there is something wrong with this bit. Is this a correct assumption? And do you see anything wrong with this? Thanks

Pulsar

function findOffset(alphabet, ch)
{
var findOffset = alphabet.indexOf(ch) - 1

If (findOffset == -1) {
return -5
} else {
return findOffset
}

}

The part that gives me trouble is the If...Else statement. I threw the rest in to give you some context. Thx.
 
Looks like a nasty capital I in If...


If (findOffset == -1) {

Looks like a variable has same name as the function ...


function findOffset(alphabet, ch)
{
var findOffset = alphabet.indexOf(ch) - 1

 
Your logic has a flaw in it, too. Because you subtract 1 from the index of a character, you could possibly get a value of -2, if the character you're checking for is not in the string called alphabet.

Lee
 
Thanks, changing the capital I to lowercase and changing the variable name fixed it. I'm porting this from VB where a variable of the same name as the function is returned, so I guess that's how that slipped in there.

Pulsar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top