...

View Full Version : newbie question; check string after a for loop



give_it_a_lash
08-26-2010, 04:30 PM
Function findsometing()
(


var myString = 'results' + '<BR>'

for (var initiator, test condition,incrementor.)
{
if (.....)
{
myString ='some quote';
}
else if (.....)
{
myString += something + '<br>'
}
else if (....... )
{
myString += something + '<br>'
}
else
{
myString += '';
}
}

displayMessage(myString)
)


I am a complete newbie to javascript and programming, so excuse any faux pas, but I want to check the myString variable after all the loops in the for loop statement have been completed. For example if nothing was added to the var myString after the completeion of the for loop, i want to display a message that no results found. i tried various attempts of an if statement along lines of

}
}

if (myString = 'results') ....i also tried if myString= ('results' + '<BR>')
{
displayMessage('no results found')
}
else
{
displayMessage(myString)
}
)

Any pointers as to where i am going wrong?

john_collins
08-26-2010, 06:51 PM
Use == instead of = when using if statement:

if (myString == 'results')

instead of:

if (myString = 'results')

Old Pedant
08-26-2010, 07:22 PM
Also, because you initialized the string to

var myString = 'results' + '<BR>'

that's what you need to check for:


if ( myString == ( 'results' + '<BR>' );

A simpler way, in general, would be to do this:


var myString = 'results' + '<BR>'
var original = myString;
... lots of possible changes ...
if ( myString == original )
{
...
}

give_it_a_lash
08-27-2010, 09:52 AM
cheers to both of you, its really annoys me when i miss small syntax things like that



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum