Help with Javascript Issues

Discussion in 'Web Programming' started by Anet390, Jun 1, 2016.

Help with Javascript Issues
  1. Unread #1 - Jun 1, 2016 at 4:46 PM
  2. Anet390
    Joined:
    Jun 3, 2010
    Posts:
    2,223
    Referrals:
    1
    Sythe Gold:
    291
    Cryptocurrency Discussion Participant Paper Trading Competition Participant

    Anet390 Grand Master
    $5 USD Donor New

    Help with Javascript Issues

    Instead of posting a new thread for every problem I may encounter, I will just continue replying to this one with any other issues I may have. Thanks.


    EDIT: I fixed my first code error. I have another one I am trying to solve by myself but I will post here again if I have issues.
     
    Last edited: Jun 1, 2016
  3. Unread #2 - Jun 1, 2016 at 8:11 PM
  4. iHateQuesting
    Joined:
    Nov 14, 2015
    Posts:
    9,882
    Referrals:
    27
    Sythe Gold:
    1,109

    iHateQuesting Hero

    Help with Javascript Issues

    Please post a screenshot of the error so i may assist.
     
  5. Unread #3 - Jun 2, 2016 at 12:34 AM
  6. Anet390
    Joined:
    Jun 3, 2010
    Posts:
    2,223
    Referrals:
    1
    Sythe Gold:
    291
    Cryptocurrency Discussion Participant Paper Trading Competition Participant

    Anet390 Grand Master
    $5 USD Donor New

    Help with Javascript Issues

    @iHateQuesting - The first screenshot is the code and the second is the result of the console.logs. I attempt to buy 10 phones and deposit 500 extra. The value should be $0.10 not $0.10000000000002274 left.

    My other question is regarding the syntax I used. For the second if statement that checks if your deposit is enough I had to manually enter them as Number(var) or the code would break but in the if statement earlier it automatically converts them to numbers. Is there a better way to do the same thing? How could you improve this code?

    [​IMG]

    [​IMG]
     
    Last edited: Jun 2, 2016
  7. Unread #4 - Jun 2, 2016 at 6:09 AM
  8. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    465
    Sythe Gold:
    5,271
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Help with Javascript Issues

    In any computer code you will have rounding errors due to the way floating point numbers work.

    If you want to make your monetary values precise to a single cent simply work in cents rather than dollars (so that you are always dealing with integer values) and convert it only upon display.
     
    ^ Anet390, SuF and iHateQuesting like this.
    Last edited: Jun 2, 2016
  9. Unread #5 - Jun 2, 2016 at 11:43 AM
  10. Program
    Joined:
    Sep 17, 2015
    Posts:
    5,003
    Referrals:
    0
    Sythe Gold:
    902
    Discord Unique ID:
    171517906276843520
    Discord Username:
    FuukinAndy #6867

    Program Formerly known as Andy Samberg
    Retired Sectional Moderator

    Help with Javascript Issues

    Just glanced at your code, but it looks to me your second "if" statement is coming into play and you didn't format it like you did in your log in the "else." So as expected it would print all the decimal points. Just add the ".toFixed" to the second "if" and you're rock n rollin.

    As far as using number(). You don't need to do that if the variable is a number, only if it's a string, or boolean etc. Which with simple stuff like this, there should be no reason for you to have a number in a string format. That's my only real complaint with your code, you use number() more than necessary. Just make sure all your values are numbers from the start and delete all those.

    Also idk why BALANCE is in caps. Always use camel-case for naming variables. I'm not a fan of the spaces in the parentheses either. Less code/space while still being readable the better. Personal preference stuff though.

    Here's your code with a few edits I made. I could still do a lot to make it better, but I was trying to keep what you've got going on intact:
    [​IMG]
    [​IMG]
     
    ^ Anet390 likes this.
    Last edited: Jun 2, 2016
  11. Unread #6 - Jun 3, 2016 at 12:32 AM
  12. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    465
    Sythe Gold:
    5,271
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Help with Javascript Issues

    Works but it's a bad solution.

    You're dealing with an integer quantity of things (in this case: cents) so you should be using integers. These are exact. That is: You can exactly express any integer in binary, no problem. Other than a few special cases decimal fractions cannot be exactly expressed in binary. When you're dealing with money which needs to be exactly correct you should use an exact computational solution.

    If you're dealing with scientific data which is imprecise to begin with then you can use floating points or some other representation.
     
    ^ Anet390 likes this.
  13. Unread #7 - Jun 3, 2016 at 12:51 AM
  14. Program
    Joined:
    Sep 17, 2015
    Posts:
    5,003
    Referrals:
    0
    Sythe Gold:
    902
    Discord Unique ID:
    171517906276843520
    Discord Username:
    FuukinAndy #6867

    Program Formerly known as Andy Samberg
    Retired Sectional Moderator

    Help with Javascript Issues

    Baby steps mate :p At least for me it was easier to not worry about the nitty gritty precision to start with. He'll learn that stuff as he gets further along. Overload too many things early on and it's hard to keep things straight. That's been my experience anyway. That's why I left most of his code as it was. He knows what .toFixed is and does and for this practice it gets the job done fine. If we want to get nitty gritty I'd delete everything he has and it wouldn't look anything like it does lol.
     
  15. Unread #8 - Jun 3, 2016 at 12:58 AM
  16. Anet390
    Joined:
    Jun 3, 2010
    Posts:
    2,223
    Referrals:
    1
    Sythe Gold:
    291
    Cryptocurrency Discussion Participant Paper Trading Competition Participant

    Anet390 Grand Master
    $5 USD Donor New

    Help with Javascript Issues

    Both of you were helpful in different ways.

    Sythe explained why it was displaying the decimal and suggested a way to get rid of that all together by setting a penny ($0.01) = 1 rather than $1 = 1, which makes sense.

    Andy, you cleaned up my code and introduced me to parseInt, which solved my massive Number(var) problem.

    I appreciate all the advice from both of you. I'm still pretty new to coding and I'm a slow learner so there's many more errors and bad code to come.
     
  17. Unread #9 - Jun 3, 2016 at 1:04 AM
  18. Program
    Joined:
    Sep 17, 2015
    Posts:
    5,003
    Referrals:
    0
    Sythe Gold:
    902
    Discord Unique ID:
    171517906276843520
    Discord Username:
    FuukinAndy #6867

    Program Formerly known as Andy Samberg
    Retired Sectional Moderator

    Help with Javascript Issues

    Sythe did make a very valid point and definitely is something you should remember.

    My advice from my little coding journey is what you seem to already have a grasp on. Just don't take on too much info at once to where it boggles your mind. You set up some excellent logic in your code man. Build on your knowledge base piece by piece and not all at once and you'll be way ahead because of it.
     
    ^ Anet390 likes this.
    Last edited: Jun 3, 2016
< Cool | >

Users viewing this thread
1 guest


 
 
Adblock breaks this site