Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: new to JavaScript, I can't get this to work  (Read 2969 times)

0 Members and 1 Guest are viewing this topic.

Computer_Hopeless

    Topic Starter


    Beginner

  • Plz pm me if you know how to transparentize this.
    new to JavaScript, I can't get this to work
    « on: July 12, 2007, 10:00:51 AM »
    I'm learning JavaScript (with the aid of online tutorials) and can't figure out where I messed up. This is what I have and where the problem seems to be:

    <script type="text/javascript">
    var apples = 10;
    alert('There are currently ' + apples + ' apples!');

    var toss = prompt('How many apples would you like to throw at the doctor?', '0');

    var thrown = parseInt(toss);
    if(thrown > apples){
       alert('Sorry, but there are not that many apples.  You can not throw ' + thrown + ' apples!');
    } else { if(apples - thrown = 1){
          alert('Now there is only one apple left to throw!');
       } else {apples -= thrown;
          alert('Now there are only ' + apples + ' apple(s) left to throw!');
          }

    }
    </script>


    Is there some weird rule about having if/else statements within other if/else statements? Or did I mess up in my ignorance elsewhere?

    Any help would be greatly appreciated.
    "An eye for an eye makes the whole world blind."
                     - Mahatma Gandhi

    "Death is the solution to all problems. No man - no problem."
                     - Josef Stalin

    michaewlewis



      Intermediate
    • Thanked: 26
      • Yes
      • Yes
    • Experience: Expert
    • OS: Unknown
    Re: new to JavaScript, I can't get this to work
    « Reply #1 on: July 12, 2007, 02:35:03 PM »
    you're missing an equals when checking if there's only one apple left
    change to this: if(apples-thrown==1)

    Computer_Hopeless

      Topic Starter


      Beginner

    • Plz pm me if you know how to transparentize this.
      Re: new to JavaScript, I can't get this to work
      « Reply #2 on: July 14, 2007, 09:29:07 AM »
      Thanks a million. I had no clue I had to use a double =, that'll be quite helpful not only with this script but with many others in the future. Thank you again.
      "An eye for an eye makes the whole world blind."
                       - Mahatma Gandhi

      "Death is the solution to all problems. No man - no problem."
                       - Josef Stalin