#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    What did I do wrong here?


    Hey everyone I am using javascript and I am basically making a budget program using a loop. First the person states their budget and write in an item and a price until they reach ten dollars and then they are told that they have gone over. For some reason I cant get it working but it seems to me like I at least have most of the code correct. Any help would be appreciated.





    <html>
    <head>
    <title>This is a math facts program</title>
    </head>
    <body>
    <script type="text/javascript">
    var Budget = window.prompt("Enter your budget");
    var name = window.prompt("Enter the item name");
    var price = window.prompt("Enter the item price");
    var price = 0;
    var savings = parseInt(budget - price);
    while (savings > 10)
    {
    alert("Savings before purchase " + savings);
    name = prompt("Enter the name of the item",0);
    price = prompt("Enter the price of the item",0);
    alert("savings after purchase " + savings);

    if(wksum <= 10)
    {
    document.write("You have gone overbudget")
    }

    }
    </body>
    </html>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    "My code doesn't work" doesn't tell us anything. We already knew that from that fact that you're looking for help in this forum. What we need is a concrete error description. Does the result have a small rounding error? Does your whole browser crash as soon as the script runs? Something in between?

    What I do see is a whole bunch of careless mistakes and logical errors. There's undefined and unused variables, typos, the whole math part in the loop is missing etc.

    Since fixing all those error most probably takes longer than simply rewriting the 10 lines, I suggest the following:

    1. Take a deep breath.
    2. Get clear about what you wanna do and then lay down the procedure with a diagram or pseudo code. Do not fumble with JavaScript at this point.
    3. When you know exactly how to go about the task, start writing your actual code. Do it slowly and carefully. Write the code step by step and test each portion before moving on.
    4. Now your code "works".

    You should also get familiar with the JavaScript console of your browser (usually F12 -> Console) for debugging and analyzing.

    Writing down the code in one go without planning or testing it works for very experienced programmers who rarely make mistakes. It does not work for beginners who've just begun to learn the language. So don't do it. What happens is that you'll be stuck with a bunch of broken code and no clue of what to do next.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Edit :Thanks for the advice

IMN logo majestic logo threadwatch logo seochat tools logo