As a personal project to learn more about Hisescript, I'm trying to build a calculator.
It's a very interesting project since it has a lot of flow statements and user interaction.
I'm getting really stuck and I'm in a position where I need help please.
How can I assign buttons into numbers ?
As show in the picture I want to assign the buttons One and Two to their respective integers (1,2).
I tried with declaring an Array [1, 2] then assign the index of each integer to their respective buttons. It's not working , and I cant find any other option.
Just need an hint to be able to move on.
@sawer If you put the button references in an array then they will all be indexed (starting from 0). As always it's important that you are careful about how you name your components.
@sawer I'm not sure this is the easiest approach...
It would be better to have 2 labels for the entries, 1 for the output, and buttons for plus, minus, etc...
So in the callback of the buttons you can gather the entries (parse the number otherwise they are strings), make the calculation, and output in the label.
You could also have a combobox to select the operator, and just one button for "equal" for instance.
@d-healey Thanks so much for the int.
Diving into the snippet now.
@ustk Interesting approach.
So my real intent is to use customised panels for the digits and screen.
I'm now working on the backend of the calculator, and buttons are the most accessible thing to do numbers digits and equal signs.
But thanks so much h for this. When done with the algorithm part of the calculator, ill use your idea for the gui (frontend)
I realise you might want to make a calculator WITH buttons...
Then it's a bit harder to append the digits
@ustk Yeah, I thought it was easier. I want to do it like a normal phone calculator. So either buttons or panels, they all need to be assigned to a digit (0-9).
Here's a basic calculator (less than 100 lines of code). Maybe don't look at it until you've tried to do it yourself. Use buttons for your first attempt because they make it easier to understand what's happening. Then try swapping all of the buttons for one or two panels - Christoph showed how to use a panel as a grid of buttons in one of the live streams I did (I think it was the second one).
@d-healey haha, I don't look either, I'm taking the challenge :)
@d-healey Yeah, I Won't look until I made it myself. But thanks
Thanks so much David.
But learning from your previous script I have some question:
I know it requires experience for you to create such efficient and effective code in less time, but I want to know how do you think?
How can you know that for a certain task it requires a precise tool.
Do I also need to learn a bit of logics?
@ustk Ahahah It's a challenging but cool project. I'm learning a lot!
Thanks so much guys.
Do I also need to learn a bit of logics?
This is absolutely the most important part of programming. Once you understand boolean logic, operators, and control statements (loops and ifs) then you'll be able to plan your strategy more quickly and efficiently.
But I change things as I go too. For example in my calculator example I've used two different methods for adding the input to the label, but I think the second method (getting the button text) is actually better in this example and I should have used it for both.
How and where to learn logics?
YouTube is also a good for general info - https://www.youtube.com/results?search_query=boolean+logic
@d-healey Thanks a lot!
@sawer Add a label to display the output and add an = button that when pressed carries out the calculation.
Given some tome to try understanding your code:
- How did you come up with he appendInput function , especially the "+="
- What's eval? There's no this such as that in the Api.
I'm learning a lot. Thanks
@d-healey Thanks David.
So I realised that I'm trying to solve problems that are bigger than me. This project is bigger than me. I'm pausing it for now and and solve easier problems while getting more of the logical thinking. Once I get more experience on this, I'll come back with more knowledge and more confidence to solve.
Thanks a lot.