Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Major Javascript help needed!
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Mon Feb 05, 2018 6:27 pm    Post subject: Major Javascript help needed! Reply with quote

I'm trying to get the amount of income that will be charged at 40% (the upper tax bracket) with a dropdown box with two deductions: a single income worth 3629.17 and a dual income is worth 2879.17.

So the first 2879.17/3629.17 is taxed at 20% and anything after is taxed at 40%. Fieldname7 is taxable income (gross income - expenses), fieldname8 is the dropdown with the two deductions

This is not returning anything for some reason:
Code:
+prec((function(){
if(fieldname8 == 2879.17 && 2879.17 < fieldname7) return 0;
if(fieldname8 == 2879.17 && fieldname7 < 2879.17) return fieldname7 - 2879.17 * 0.4;
if(fieldname8 == 3629.17 && 3629.17 < fieldname7) return 0;
if(fieldname8 == 3629.17 && fieldname7 < 3629.17) return fieldname7 - 3629.17 * 0.4;
})(),2)


I've stopped seeing anything and it's now just a jumble of letters and numbers
_________________
"Sex: breakfast of champions" - James Hunt


Last edited by cokey on Tue Feb 06, 2018 8:32 pm; edited 1 time in total
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 131
Location: India

PostPosted: Mon Feb 05, 2018 6:36 pm    Post subject: Reply with quote

Generally speaking taxable income is gross income, i.e. income after deductions.

It is almost never in JavaScript.
_________________
"Defeat is a state of mind. No one is ever defeated, until defeat has been accepted as a reality." -- Bruce Lee
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Mon Feb 05, 2018 6:44 pm    Post subject: Reply with quote

notageek wrote:
Generally speaking taxable income is gross income, i.e. income after deductions.

It is almost never in JavaScript.
Gross income is revenue minus costs. A taxable income is gross income minus deductions (like expenses, for example). Net income is gross - deductions - tax

I have seen gross javascript...

Anyway, where am I going wrong?
_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
nokilli
Apprentice
Apprentice


Joined: 25 Feb 2004
Posts: 185

PostPosted: Mon Feb 05, 2018 6:47 pm    Post subject: Reply with quote

I've just found something I hate more than doing my taxes.
_________________
Today is the first day of the rest of your Gentoo installation.
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Mon Feb 05, 2018 6:48 pm    Post subject: Reply with quote

nokilli wrote:
I've just found something I hate more than doing my taxes.
You haven't seen a later one...
Code:
+prec((function(){
if(fieldname7 < 1083.30){ return 0 ;}
else if(1083.30 <= fieldname7 && fieldname7 < 1614.33) { return 1001*0.005+(fieldname7-1001)*0.02 ;}
else if(1614.33 <= fieldname7 && fieldname7 < 5837) {
return 1001*0.005+613.33*0.02+(fieldname7-1614.33) * 0.0475}
else if(5837 <= fieldname7 && fieldname7 < 8333.33) {
return (1001*0.005)+(613.33*0.02)+(4222.67*0.0475)+((fieldname7-5837)*0.08) }
{ return 2496.33*0.08+(4222.67*0.0475)+(613.33*0.02)+(1000*0.005)+(fieldname7-8333.33)*0.11;
}
})(),2)

_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 131
Location: India

PostPosted: Mon Feb 05, 2018 6:55 pm    Post subject: Reply with quote

cokey wrote:
notageek wrote:
Anyway, where am I going wrong?
Getting paid in JavaScript?
_________________
"Defeat is a state of mind. No one is ever defeated, until defeat has been accepted as a reality." -- Bruce Lee
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Mon Feb 05, 2018 6:59 pm    Post subject: Reply with quote

notageek wrote:
cokey wrote:
notageek wrote:
Anyway, where am I going wrong?
Getting paid in JavaScript?
No, Euros. Just as worthless
_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 131
Location: India

PostPosted: Mon Feb 05, 2018 7:25 pm    Post subject: Reply with quote

cokey wrote:
notageek wrote:
cokey wrote:
notageek wrote:
Anyway, where am I going wrong?
Getting paid in JavaScript?
No, Euros. Just as worthless
There's your problem. Mark the thread as solved. You're welcome.
_________________
"Defeat is a state of mind. No one is ever defeated, until defeat has been accepted as a reality." -- Bruce Lee
Back to top
View user's profile Send private message
nokilli
Apprentice
Apprentice


Joined: 25 Feb 2004
Posts: 185

PostPosted: Mon Feb 05, 2018 7:29 pm    Post subject: Reply with quote

Code:
+prec((function(){
if (fieldname7 <= fieldname8) return 0; else return (fieldname8 - fieldname7) * 0.4;
})(),2)

_________________
Today is the first day of the rest of your Gentoo installation.
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Mon Feb 05, 2018 7:43 pm    Post subject: Reply with quote

nokilli wrote:
Code:
+prec((function(){
if (fieldname7 <= fieldname8) return 0; else return (fieldname8 - fieldname7) * 0.4;
})(),2)
Swap the field names over and that looks bang on! Thanks! Kudos++ for you!

Scratch that, it didn't work, lol. Should have checked it more
_________________
"Sex: breakfast of champions" - James Hunt


Last edited by cokey on Mon Feb 05, 2018 9:27 pm; edited 1 time in total
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Mon Feb 05, 2018 7:49 pm    Post subject: Reply with quote

Also, what this world needs is an automatic Excel cell function to Javascript translator.

There's a million pound idea!
_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 131
Location: India

PostPosted: Mon Feb 05, 2018 8:48 pm    Post subject: Reply with quote

Already invented. Your Google foo is very weak.

Here are three random links:

https://github.com/sutoiku/formula.js/

https://github.com/joshbtn/excelFormulaUtilitiesJS

https://www.npmjs.com/package/excel-formula
_________________
"Defeat is a state of mind. No one is ever defeated, until defeat has been accepted as a reality." -- Bruce Lee
Back to top
View user's profile Send private message
zixnub
n00b
n00b


Joined: 27 Dec 2007
Posts: 67
Location: Brasschaat, Belgium

PostPosted: Mon Feb 05, 2018 8:52 pm    Post subject: Reply with quote

dude, use jsfiddle because your js looks unreadable
_________________
https://github.com/udevbe/greenfield
https://github.com/udevbe/westfield
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Tue Feb 06, 2018 12:33 am    Post subject: Reply with quote

notageek wrote:
Already invented. Your Google foo is very weak.

Here are three random links:

https://github.com/sutoiku/formula.js/

https://github.com/joshbtn/excelFormulaUtilitiesJS

https://www.npmjs.com/package/excel-formula
Don't work. It renders
Code:
=ROUND(IF(E10<165,(E10*M10)/N10,IF(E10<1629.01,(E10*M12)/N12,(E10*M13)/N13)),2)
to
Code:
ROUND((E10<165,(E10*M10)/N10,IF(E10<1629.01,(E10*M12)/N12,(E10*M13)/N13)),2)
which is useless.
_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 131
Location: India

PostPosted: Tue Feb 06, 2018 12:37 am    Post subject: Reply with quote

:lol: :lol: :lol:
_________________
"Defeat is a state of mind. No one is ever defeated, until defeat has been accepted as a reality." -- Bruce Lee
Back to top
View user's profile Send private message
tylerwylie
Guru
Guru


Joined: 19 Sep 2004
Posts: 458
Location: /US/Georgia

PostPosted: Tue Feb 06, 2018 5:55 am    Post subject: Reply with quote

You can just use import taxationIsTheft and run one of the methods from that module I believe.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 17508

PostPosted: Tue Feb 06, 2018 6:41 am    Post subject: Reply with quote

:lol:
_________________
It is what it is out there. So whatever it is, it is.
Back to top
View user's profile Send private message
Akkara
Administrator
Administrator


Joined: 28 Mar 2006
Posts: 6522
Location: &akkara

PostPosted: Tue Feb 06, 2018 8:17 am    Post subject: Re: Taxable income in Javascript Reply with quote

cokey wrote:
Code:
+prec((function(){
if(fieldname8 == 2879.17 && 2879.17 < fieldname7) return 0;
if(fieldname8 == 2879.17 && fieldname7 < 2879.17) return fieldname7 - 2879.17 * 0.4;
if(fieldname8 == 3629.17 && 3629.17 < fieldname7) return 0;
if(fieldname8 == 3629.17 && fieldname7 < 3629.17) return fieldname7 - 3629.17 * 0.4;
})(),2)


I've stopped seeing anything and it's now just a jumble of letters and numbers

I just skimmed this thread quickly so this may have been answered, but one thing stood out I need to write about:

Are those floating-point numbers?

Binary floating-point (which, if I recall, is what JS uses), cannot represent most decimals exactly. And you are doing '==' comparison, which means all the bits need to be identical. Which, odds are, they won't be if the two sides were arrived at via different computations, even if those computations are mathematically equivalent..

Usually the way these things are done is using less-than operators and binary search to find what applies. Or, if the list is small enough, just a linear series of less-than comparisons against a increasing series of breakpoints will do.

You can also multiply by 100 and round to integer, then do comparisons. Integer arithmetic is exact.
_________________
Humility means not having to eat as much crow when you are shown to be wrong.
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Tue Feb 06, 2018 8:26 am    Post subject: Reply with quote

tylerwylie wrote:
You can just use import taxationIsTheft and run one of the methods from that module I believe.
I've never understood the tax is theft and social security is a pyramid scheme ideas.Without them you'd be speaking Russian
_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
nokilli
Apprentice
Apprentice


Joined: 25 Feb 2004
Posts: 185

PostPosted: Tue Feb 06, 2018 8:53 am    Post subject: Reply with quote

cokey wrote:
tylerwylie wrote:
You can just use import taxationIsTheft and run one of the methods from that module I believe.
I've never understood the tax is theft and social security is a pyramid scheme ideas.Without them you'd be speaking Russian

So long as it isn't JavaScript.
_________________
Today is the first day of the rest of your Gentoo installation.
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Tue Feb 06, 2018 8:59 am    Post subject: Reply with quote

Ok, I have an Excel table that looks like this:
Code:
PRSI BANDS       Monthly Earnings          Employee     Employer       For Employer
               
                 From         To         Contribution   Contribution     Formula
  JO             0.00       164.99          0.00%         0.50%           1.005
  AO            165.00      1,525.00        0.00%         8.60%           1.086
  AX           1,525.01     1,629.00        4.00%         8.60%           1.086
  A1           1,629.01    100,000.00       4.00%         10.85%          1.1085

Do I just create multiple arrays? like
Code:
var array monthlyEarnings [];
for (var i = 0.00; i <= 164.99; i++)
for (var i = 165.00; i <= 1525.00; i++)
for (var i = 1,525.01; i <= 164.99; i++)
for (var i = 1,630.00; i <= 100,000.00; i++) {
list.push(i);
}

There has to be a better way
_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Tue Feb 06, 2018 9:03 am    Post subject: Reply with quote

nokilli wrote:
cokey wrote:
tylerwylie wrote:
You can just use import taxationIsTheft and run one of the methods from that module I believe.
I've never understood the tax is theft and social security is a pyramid scheme ideas.Without them you'd be speaking Russian

So long as it isn't JavaScript.
javascript iz tha shizzle!

Nah, it's the best (and pretty much only) way to get complex functions into calculated fields. Oh, and as you can tell, javascript ain't my thing, I'm pretty much learning it as I go.
_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
zixnub
n00b
n00b


Joined: 27 Dec 2007
Posts: 67
Location: Brasschaat, Belgium

PostPosted: Tue Feb 06, 2018 9:23 am    Post subject: Re: Taxable income in Javascript Reply with quote

Akkara wrote:
cokey wrote:
Code:
+prec((function(){
if(fieldname8 == 2879.17 && 2879.17 < fieldname7) return 0;
if(fieldname8 == 2879.17 && fieldname7 < 2879.17) return fieldname7 - 2879.17 * 0.4;
if(fieldname8 == 3629.17 && 3629.17 < fieldname7) return 0;
if(fieldname8 == 3629.17 && fieldname7 < 3629.17) return fieldname7 - 3629.17 * 0.4;
})(),2)


I've stopped seeing anything and it's now just a jumble of letters and numbers

I just skimmed this thread quickly so this may have been answered, but one thing stood out I need to write about:

Are those floating-point numbers?

Binary floating-point (which, if I recall, is what JS uses), cannot represent most decimals exactly. And you are doing '==' comparison, which means all the bits need to be identical. Which, odds are, they won't be if the two sides were arrived at via different computations, even if those computations are mathematically equivalent..

Usually the way these things are done is using less-than operators and binary search to find what applies. Or, if the list is small enough, just a linear series of less-than comparisons against a increasing series of breakpoints will do.

You can also multiply by 100 and round to integer, then do comparisons. Integer arithmetic is exact.


Yes js uses 64-bit floating point for all it's numbers. If you need to cut off fractions you can use a zero bitshift (<<0) as that will internally convert the float to a 32-bit integer.
You could also start by calculating in cents to begin with so all arithmetic is done using integers. To visually show a number you can put a dot in the string representation.
_________________
https://github.com/udevbe/greenfield
https://github.com/udevbe/westfield
Back to top
View user's profile Send private message
cokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3343

PostPosted: Tue Feb 06, 2018 1:35 pm    Post subject: Re: Taxable income in Javascript Reply with quote

zixnub wrote:
Akkara wrote:
cokey wrote:
Code:
+prec((function(){
if(fieldname8 == 2879.17 && 2879.17 < fieldname7) return 0;
if(fieldname8 == 2879.17 && fieldname7 < 2879.17) return fieldname7 - 2879.17 * 0.4;
if(fieldname8 == 3629.17 && 3629.17 < fieldname7) return 0;
if(fieldname8 == 3629.17 && fieldname7 < 3629.17) return fieldname7 - 3629.17 * 0.4;
})(),2)


I've stopped seeing anything and it's now just a jumble of letters and numbers

I just skimmed this thread quickly so this may have been answered, but one thing stood out I need to write about:

Are those floating-point numbers?

Binary floating-point (which, if I recall, is what JS uses), cannot represent most decimals exactly. And you are doing '==' comparison, which means all the bits need to be identical. Which, odds are, they won't be if the two sides were arrived at via different computations, even if those computations are mathematically equivalent..

Usually the way these things are done is using less-than operators and binary search to find what applies. Or, if the list is small enough, just a linear series of less-than comparisons against a increasing series of breakpoints will do.

You can also multiply by 100 and round to integer, then do comparisons. Integer arithmetic is exact.


Yes js uses 64-bit floating point for all it's numbers. If you need to cut off fractions you can use a zero bitshift (<<0) as that will internally convert the float to a 32-bit integer.
You could also start by calculating in cents to begin with so all arithmetic is done using integers. To visually show a number you can put a dot in the string representation.
I set the precision to 2 decimal places with prec
Code:
+prec(9 { if foo, bar
else foobar;
})2)

_________________
"Sex: breakfast of champions" - James Hunt
Back to top
View user's profile Send private message
zixnub
n00b
n00b


Joined: 27 Dec 2007
Posts: 67
Location: Brasschaat, Belgium

PostPosted: Tue Feb 06, 2018 8:08 pm    Post subject: Re: Taxable income in Javascript Reply with quote

cokey wrote:
I set the precision to 2 decimal places with prec
Code:
+prec(9 { if foo, bar
else foobar;
})2)


Are we still talking vanilla javascript here? I've never heard of +prec, and google turns up empty as well.
_________________
https://github.com/udevbe/greenfield
https://github.com/udevbe/westfield
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum