Discuss Scratch

GameCasade
Scratcher
3 posts

Is there a way to condense my scripts?

I'm working on a tax calculator, and during the coding process I found some repeating patterns in the code that I want to condense into one singular code.

if <<<(answer) = [45000]> or <(answer) < [45000]>>> then
say ((answer) * (0.10))
end
if <<(answer) > [45000]> and <<(answer) < [60000]> or <(answer) = [60000]>>> then
say (((45000) * (0.10)) + ((0.12) * ((answer) - (45000))))
end
if <<(answer) > [60000]> and <<(answer) < [80000]> or <(answer) = [80000]>>> then
say ((((45000) * (0.10)) + ((60000) * (0.12))) + ((0.15) * ((answer) - (60000))))
end

This is the pattern the code follows for calculating each individual tax bracket, where in the second ‘say’ block, the additional 0.12(answer - 45000) means the leftover value above 45000 will be multiplied by 0.12, because the ‘if’ statement shows that this code will only run when the answer is greater than 45000 but less than 60000.

The pattern repeats for the third block, since 0.15(answer - 60000) calculates 15% of a leftover amount that is larger than the previous tax bracket, which was 60000.

Since the ‘say’ code follows a pattern, is there a way to represent this pattern instead of repeating the steps shown in the code? When I continue the pattern using the code format shown above, the code becomes indescribably long.

Last edited by GameCasade (March 5, 2023 20:28:58)

Spentine
Scratcher
1000+ posts

Is there a way to condense my scripts?

Using a few basic simplification and distribution rules we can get
if <<(answer) > (60000)> and <not <(answer) > (80000)>>> then
say ((2700) + ((0.15) * (answer)))
else
if <<(answer) > (45000)> and <not <(answer) > (60000)>>> then
say (((0.12) * (answer)) - (900))
else
if <not <(answer) > (45000)>> then
say ((answer) * (0.1))
end
end
end
but there is a way to simplify it more. Since boolean reporter blocks can be put into mathematical operations, treated as 0 if it's false and 1 if it's true, it can be further simplified to
say ((((2700) + ((0.15) * (answer))) * <<(answer) > (60000)> and <not <(answer) > (80000)>>>) + (((((0.12) * (answer)) - (900)) * <<(answer) > (45000)> and <not <(answer) > (60000)>>>) + (((answer) * (0.1)) * <not <(answer) > (45000)>>)))
Hope this helps!

Last edited by Spentine (March 6, 2023 05:49:23)

GameCasade
Scratcher
3 posts

Is there a way to condense my scripts?

Hello Spentine, this cleared up a lot, thank you!
vladfein
Scratcher
100+ posts

Is there a way to condense my scripts?

Although the single-line solution offered above is very elegant, I would like to offer an alternative. Think what you would have to go through to add a tax bracket, or change limit or rate on some of them.

The “single line” can be achieved by creating a Block that does tax calculation and calling it once for a given “answer”. Here I am trying to separate data (tax brackets and corresponding rates) from algorithm.

https://scratch.mit.edu/projects/814038362/


Last edited by vladfein (March 6, 2023 05:03:06)

GameCasade
Scratcher
3 posts

Is there a way to condense my scripts?

Hi vladfein, you gave me tons of insight into new areas of coding. Also, thank you for putting in the time to make an actual game for this!

Powered by DjangoBB