Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use == instead of = when using if statement:

    if (myString == 'results')

    instead of:

    if (myString = 'results')

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cheers to both of you, its really annoys me when i miss small syntax things like that


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •