Results 1 to 6 of 6
  1. #1
    More Cheesier Than Ever Cheesehead's Avatar
    Join Date
    January 18th, 2005
    Location
    Land of The NFL Champs!
    Posts
    2,942
    Javascript Code Question
    I am writing a "coding" program for my kids. To decode the message, they must decipher the formula. The formula can be as simple as the one shown here (add 3) or more complicated. So each letter input will make up a variable. And the variable will be modified. Problem is, every input value gets set to 1 so I ALWAYS get 4 as the output. I can't see why the if-else is always executing after the first condition.

    Please help. Thanks!

    <script type="text/javascript">

    var letter1 = prompt("Please enter the first letter.", "First Letter of Message ");

    var letter1a = letter1;

    if (letter1a = "A")
    {
    var letter1a = 1;
    }
    else if (letter1a = "B")
    {
    var letter1a = 2;
    }
    else
    {
    var letter1a = 3;
    }

    letter1a = letter1a + 3;
    document.write(letter1a)

    //-->

    </script>
    This World is Not My Home
    We're gonna go inside, we're gonna go outside, inside and outside. . . And then we're gonna go go go and we're not gonna stop til we get across that goalline! Quotes from the movie Rudy, 1993

  2. #2
    ABW Ambassador sjangro's Avatar
    Join Date
    January 18th, 2005
    Location
    Boston
    Posts
    1,529
    Cheesehead

    Use == for equality testing, not = which is assignment.

    You're assigning the value of letter1a instead of comparing it.
    The assignment always returns true, which then dumps into the first case, which sets it to 1.

  3. #3
    More Cheesier Than Ever Cheesehead's Avatar
    Join Date
    January 18th, 2005
    Location
    Land of The NFL Champs!
    Posts
    2,942
    Thanks! I suspected there was something related to syntax. I am not experienced with JS, just tinkered with it a little in the past.
    This World is Not My Home
    We're gonna go inside, we're gonna go outside, inside and outside. . . And then we're gonna go go go and we're not gonna stop til we get across that goalline! Quotes from the movie Rudy, 1993

  4. #4
    Moderator MichaelColey's Avatar
    Join Date
    January 18th, 2005
    Location
    Mansfield, TX
    Posts
    16,232
    That can be one of the trickiest mistakes to catch. I remember the first time I made that mistake, modifying some operating system code that was written in C in an "Operating Systems" class in college. It took me a couple days to find the problem.
    Michael Coley
    Amazing-Bargains.com
     Affiliate Tips | Merchant Best Practices | Affiliate Friendly? | Couponing | CPA Networks? | ABW Tips | Activating Affiliates
    "Education is the most powerful weapon which you can use to change the world." Nelson Mandela

  5. #5
    ABW Ambassador sjangro's Avatar
    Join Date
    January 18th, 2005
    Location
    Boston
    Posts
    1,529
    yeah, I'd put that one up there as one of the top ten all time programming bugs.

    Interestingly, Python doesn't allow you to do an assignment where an expression normally goes. Nice little feature to that language.

  6. #6
    Roll Tide mobilebadboy's Avatar
    Join Date
    January 18th, 2005
    Location
    Mobile, Alabama
    Posts
    1,220
    Just for the record, the same thing goes with PHP.

    Shawn Kerr (.com) | Disney World | SEC Football

  7. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. JavaScript Code - Non Spiderable version
    By westcoastnative in forum GoldenCAN
    Replies: 8
    Last Post: April 6th, 2011, 10:38 AM
  2. Another Javascript Question
    By apex-auctions in forum Programming / Datafeeds / Tools
    Replies: 4
    Last Post: April 23rd, 2006, 11:56 PM
  3. How to Place SmartZone Code Inside a JavaScript
    By huhu in forum Commission Junction - CJ
    Replies: 5
    Last Post: September 27th, 2003, 06:49 PM
  4. Javascript question
    By tmd5 in forum Programming / Datafeeds / Tools
    Replies: 6
    Last Post: March 30th, 2002, 08:38 PM

Posting Permissions

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