• Log in or sign up for an account here: https://www.sythe.org/login

Help with Javascript Issues

Anet390

Grand Master

$5 USD Donor New
Joined
Jun 3, 2010
Sythe Gold
291
Messages
2,223
Discord Verified
Cryptocurrency Discussion Participant
Paper Trading Competition Participant
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:
Discord Verified
Cryptocurrency Discussion Participant
Paper Trading Competition Participant
@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?

x6xQ0rT.png


OGYrUQV.png
 
Last edited:
Supporting Business
Discord Voice
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
PHP Shit Show 2k26
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
@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?

x6xQ0rT.png


OGYrUQV.png
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.
 
Last edited:
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:
JCdGR6t.png

9l3Brwa.png
 
Last edited:
Supporting Business
Discord Voice
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
PHP Shit Show 2k26
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
Just add the ".toFixed" to the second "if" and you're rock n rollin.

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.
 
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.

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.
 
Discord Verified
Cryptocurrency Discussion Participant
Paper Trading Competition Participant
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.
 
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.

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.
 
Last edited:
Runescape Gamblingrunescape gambling
Back
Top