Calculator only using Snap! blocks

Main

I made a calculator with only using Snap! blocks(Yesterday).

Description

  • Use the (calculate [expression] @addInput :: #800080) block to calculate expressions.
  • Press the eye icon to see the code.
  • This project performs user’s expression input without seeing in the code, and store variable definitions using foo=3,bar=1/2,baz=sqrt(2)... form.

Project

Supported functions

  • Four main arithmetic functions: +, -, *, /
  • Powers (Exponents) and roots: ^, exp(x), sqrt(x), cbrt(x)
  • 12 main tirgonometric functions: sin(x), cos(x), tan(x), sec(x), csc(x), cot(x)
    • Add arc to the six main trigonometric functions to access inverse trigonometric functions.
  • Logarithms: ln(x), log(x)
  • Rounding: floor(x), ceil(x), round(x)

Warning

  • Unary negation only applies to numbers. Use -1* to perform unary negation for function calls, subexpressions and variables.
  • By the reason at the top, the block (calculate [-2^2] @addInput :: #800080) calculates (-2) ^2 becuase the unary negation is applied to 2, not 2^2. Use (calculate [-1*2^2] @addInput :: #800080) to handle this.

I really like the project. Your code is very readable. However, I noticed some when <f> blocks in the sprites. snap checks to see if <false is true over and over again multiple times each second. As a result, I would suggest instead using no hat block rather than a when <f> .

Thanks, I fixed it.

I like the project, but I had the opposite reaction about the readability of the code. I can’t even find the code! I mean, yes, I get it, the actual code that does the work is in fields of a JSON object. But it’s really difficult even to see the code, and impossible to edit it.

I’m not convinced that this project calls for OOP at all. The hallmark of an OOP project is that you clone the objects. But you just have a handful of objects with names like “calculator.” The fields of those objects are methods, and never change; this is why you have that IF FALSE script that runs initialization code, that has to be clicked before using the project.

So, first of all, a hint about initialization:


Uploading: Calculator using only Snap! blocks script pic (3).png…

But it’s not as if clicking the green flag actually does anything. The biggest weakness of this project is that it doesn’t have a user interface. Even a simple one like this would help:

So, about OOP, if it were my project I’d use plain old procedures, not object methods, to do the work. Really you’re using the OOP system as a package system, not taking any advantage of inheritance. If what you want is a package system, while still having the code visible and editable, I’d use sprite OOP, not JSON OOP. Have a sprite named CALCULATOR, for example, and define sprite-local procedures for it in the Block Editor:



That would be working with the strengths of Snap! rather than fighting against them.

I have changed the project to perform user’s expression input without seeing in the code, and store variable definitions using foo=3,bar=1/2,baz=sqrt(2)... form.