Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Javascript Help

  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Arrow Javascript Help

    Hey guys noob here! Was hoping someone could point me in right direction as to why my check for an empty field is not getting caught when I run the code...


    var yourBill=prompt("How Much Was Your Bill?");
    var yourPayment;
    var yourChange;

    yourPayment = parseInt(yourPayment);
    yourChange = parseInt(yourChange);
    yourBill = parseInt(yourBill);

    if (yourBill===null || yourBill==="")
    {
    alert("Enter a cash amount!")
    }
    else if (yourBill<=0)
    {
    alert("Bills aren't free!");
    }
    else (yourBill>0)
    {
    yourPayment=prompt("How Much Did you hand the cashier?");
    yourChange = yourPayment - yourBill;
    document.writeln("You are owed " + yourChange);
    }

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 633 Times in 623 Posts
    Is this homework for a course you asre doing? If not then I suggest scrapping the whole thing and starting over as most of the code you have there is effectively obsolete.

    document.write should never be used - it was replaced by innerHTML about 10 years ag0. prompt and alert were for debugging but have been replaced by console.log and the built in debugger. parseInt is for converting numbers between bases eg hex to decimal. - the Number function converts strings to numbers.

    If it is for a "History of JavaScript" course you are doing then the following three statements will always result in the if statement evaluating as false as the value yourBill will have if they didn't enter anything is 0 and never null or ""

    var yourBill=prompt("How Much Was Your Bill?");
    yourBill = parseInt(yourBill);
    if (yourBill===null || yourBill==="")



    It will never be null and will only be "" if you test it before trying to convert it to a number.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    javascript_noob (02-12-2014)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Try

    yourBill = Number(yourBill) || 0;


    and delete

    yourPayment = parseInt(yourPayment);
    yourChange = parseInt(yourChange);

    as they have no values at this stage.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    javascript_noob (02-12-2014)

  • #4
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi felgall yes it's homework for a night time course I'm currently enrolled on. Most of the coding I came up with is from a book called AJAX, Rich Internet Applications, and Web Development for Programmers. Thanks for your help I'll give that a try and see what happens.

    Thanks Philip M I'll try that as well.


  •  

    Posting Permissions

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