Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-26-2010, 04:30 PM   PM User | #1
give_it_a_lash
New to the CF scene

 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
give_it_a_lash is an unknown quantity at this point
newbie question; check string after a for loop

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?
give_it_a_lash is offline   Reply With Quote
Old 08-26-2010, 06:51 PM   PM User | #2
john_collins
New to the CF scene

 
Join Date: Aug 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
john_collins is an unknown quantity at this point
Use == instead of = when using if statement:

if (myString == 'results')

instead of:

if (myString = 'results')
john_collins is offline   Reply With Quote
Old 08-26-2010, 07:22 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Also, because you initialized the string to
Code:
var myString = 'results' + '<BR>'
that's what you need to check for:
Code:
if ( myString == ( 'results' + '<BR>' );
A simpler way, in general, would be to do this:
Code:
var myString = 'results' + '<BR>'
var original = myString; 
... lots of possible changes ...
if ( myString == original ) 
{
   ... 
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-27-2010, 09:52 AM   PM User | #4
give_it_a_lash
New to the CF scene

 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
give_it_a_lash is an unknown quantity at this point
cheers to both of you, its really annoys me when i miss small syntax things like that
give_it_a_lash is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:42 PM.


Advertisement
Log in to turn off these ads.