www.webdeveloper.com
Results 1 to 8 of 8

Thread: Alert message not what I expected

  1. #1
    Join Date
    Feb 2012
    Posts
    6

    Alert message not what I expected

    Hello,

    I am trying to learn JavaScript and I have set up the code below. In my Alert dialog when I run it, I get the value 600. I am trying to get it to say the sentence..

    "high score belongs to (either playerOne or playerTwo)....instead the alert says 600.
    can someone tell me what I am doing wrong??
    thanks
    babs


    var playerOne=500;
    var playerTwo=600;

    var highScore;

    if (playerOne>playerTwo){
    highScore=playerOne;
    }
    else {
    highScore=playerTwo;
    }

    alert ("high score belongs to " && highScore);



    thanks!

  2. #2
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    HTML Code:
    <script>
    var playerOne=500;
    var playerTwo=600;
    
    if (playerOne>playerTwo){
    alert("high score belongs to playerOne!");
    }
    else {
    alert("high score belongs to playerTwo!");
    }
    </script>

  3. #3
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    HTML Code:
    var playerOne=500;
    var playerTwo=600;
    
    var highScore;
    
    if (playerOne>playerTwo){
    highScore="playerOne";
    }
    else {
    highScore="playerTwo";
    }
    alert ("high score belongs to " + highScore);
    Use + instead of &&
    "It will never rain roses: when we want to have more roses, we must plant more roses." - George Eliot

  4. #4
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    Note: When you assign a text value to a variable, use quotes around the value.
    Use:
    Code:
    highScore="playerOne";
    Instead of:
    Code:
    highScore=playerOne;
    Check: http://www.w3schools.com/js/js_variables.asp

  5. #5
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    The + operator is used to add string variables or text values together.

    To add two or more string variables together, use the + operator.

    && is not used to do that!


    Check: http://www.w3schools.com/js/js_operators.asp

  6. #6
    Join Date
    Feb 2012
    Posts
    6
    Hello!

    Thank you so much for all of this....
    very helpful...
    I will also check out the W3C site you have recommended!
    thanks
    babs

  7. #7
    Join Date
    Feb 2012
    Posts
    6
    Hello!

    Thank you so much for all of this....
    very helpful...
    I will also check out the W3schools site you have recommended!
    thanks
    babs

  8. #8
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194

    Thumbs up

    Quote Originally Posted by ibabs View Post
    Hello!

    Thank you so much for all of this....
    very helpful...
    I will also check out the W3C site you have recommended!
    thanks
    babs
    You are welcome, dear!

    "It will never rain roses: when we want to have more roses, we must plant more roses." - George Eliot

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles