SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,315
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Stumped on simple alert() message creation

    I'm seeking to advance my knowledge of Javascript with a book. But I can't figure out why the alert() message does not fire:

    Code:
    <script type="text/Javascript">
    var result = 2 * 4;
    var message = "2 times 4 = " result;
    alert(message);	
    </script>
    Steve Husting

  2. #2
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    You have two previous instructions. Which one do you think is the wrong one?

    The first one evaluates to a number: 2*4. The second one evaluates to a... what?

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,315
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here is how I understand it:

    var result = 2 * 4; // this should give me 8.
    var message = "2 times 4 = " result; // this should give me the string of 2 times 4 and concatenate the result of 8.
    alert(message); // the var above of string and "8" should appear in the alert box.
    Steve Husting

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    The concatenation is wrong. You're missing an operator

    Code:
    var message = "2 times 4 = " <something missing here> result;

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,315
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I see it! I need a plus sign.

    var result = 2 * 4;
    var message = "2 times 4 = " + result;
    alert(message);

    Thank you very much!
    Steve Husting

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    No probs


Bookmarks

Posting Permissions

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