Calculator Project
-
Hello everyone!
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.
Thanks. -
@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.
HiseSnippet 897.3ocuVEtaaaCDlzIJcRaqnEnO.D8WJHctxsoYCncXswIdvXMIFycECnnnfghJlHTjBjTY0nH+sOZ84YuAaGkTrbbLxZLvh9gMu69NceG4w6zHilwsVsAgCeyzBNB+cAimpbS5OgJTng6gv2M3.p0wMjZU6NsfZs7TDFu1u5UfCWGU872+xtTIUw3spPn2pEL9qE4BWq1Qu72DR4.ZJ+Mh74Pu8KGxzp9ZotD3yZAInBJ6T5I7CodXcBP3M1OU3zlwNpiaAL6pSmNdh9uT03eqvJNVx8B8PigWTsZT+IBY5nKxUKBgWeTaluVcl+ffCDohY5a2AtWkARqGyuGf6bcTp2MfR34nz50T59AiYFQgq0hmOeavPEbfjQgs54oRMVTm6zInuFPnbcyomxGX.gYdDuSRxiHvOa97nnG+XxwN0gk4GyMQvdu00JS9Yx6dOfISaHwBPJ44DA4Ejcf+1ZqMi9TT3LrcKJsShuHnmvc804EZEHD+vYfdHYKhXSHrs98Nw66Z8nUNiV1mJkGCm3wZ0LDMl.uNOJRnjBEmjUpXNgVQtJtX1EA9QjynxRdEOkZFURDpT9Gg7nk0UZNJq0IO4hBEYj3FmCAuCg2sUK4cKLBHgpeMaQ54AedXz4yQmC0N9Qp3pfBDlrnorrkZqg7RtYol82RLWmiwpp7Y9T1CDJitbs4Fec0lr5yw4.pUCUB2QE7F4AZYpulyu9pUxnlBAX0eLbOpi5KtazA3J3FmvSG7d7yftC0k5gA6wsm5zE.MmU8fv2wUY8tMWD1sz4zJjH02nZ1AYB5iHb.NFB3T+hAvBK8L9P0HCGpu7WND1Cz4vqjZ.H3NbEExb+85WyoFkGA5lF1dKD1Oe6D1mrPX+xsSXe5BgcC7sRX2dwy1amv9rEOaWsvd0V5vvDcZoj5t7DF+nzFCvU6K0V225VYEtoyOp8FL1I4ZG670Rw6GLR3XSVNG6rDNBW5++fiMCq+9f8yx3LWKAWOXvetpSl+OB+uqKcB0IGPcFgur.JQFCeiBiCQWo3RquBoiuwWsbhW1uCLlqRqD9G3owXOuLtwXuKLhxoLi9Cr5F59OG3apz.bRU80OgvmgAxjdnpl7feAIcSP4P41GXLe5+CPu3k6ySVAed5J3y1qfOOaE7YmUvmebE74mtVe72zeUoSmWec.TLZ+pocX79UsBppBQ+KP0WHxt
-
@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) -
@ustk said in Calculator Project:
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).
HiseSnippet 1347.3ocuXttaaaCE.lxIJa1csqEXO.B9WJKoI14dQav7hS7fwxEi4thATzUPKQaSDZROIpj3sUf8NrWn9nrGg9FrcntXQmXkXaL07ijb34B+3gWNhrkmvg36K7PFEe8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xWVqN+rtUV407l0sleV2Vi0OE7Yj0smeV2Iq0.+yk4Kq6L+rtaVqAxaV2c9YcurVCj2rt27y59YsF3icxWV2e9Y8frVCj2rdv7y5KxZMPdy5KlQVeho12ZpW4x5O+LR6lKBsUyh17d+02tHztUVzl2UDd9hP61YQad+MLqMiz9Hyw2eHqsX4MpGNinVxL4QAx5P17kTSy5Luow5iiY8TbGBK9jqzKbneJaGMXQmWKtqMM9UCDJoaMMdEnDyn83CB6kuvzKTSW3asaS+cs2daqXxRjKWqSujWhq3JPC+czS2cmmk5olmIbiddP8WIS8zfwJfaeLwSSod9ItOUNR+oC+e6oylUDelYKpzo+zYrvTXDtiRdvX7CN9XyS51k3HSAbYyF+R975hneRDHo7dmgkdTXIkITXrMLS6PfdmyIL0s+LJntmVjbEkrJCzFt2cnv+B+DqrpR1HVY0DknAXGOw6chtyo5IM+xvV.l3gulaQyyTxVUQg2CUsmnxFUPCf8Su2wQM7eNbgjo6yVKfOau.9ryB3ytKfO6s.9r+B3yA2qOpix99.oXPz1AngVmDd4bCiSBOqKbUH5+vsCMIZ
-
@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. -
@sawer said in Calculator Project:
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.
-
@d-healey said in Calculator Project:
This is absolutely the most important part of programming.
Thanks.
How and where to learn logics? Wanna do that on a daily basis to be more proficient in thinking. -
@sawer said in Calculator Project:
How and where to learn logics?
There are some links to Javascript tutorials in the HISE docs which are a good place to start. Once you get the basics you need to read and write lots of code to practice and get it to become a natural part of your thinking when you're presented with a programming challenge.
YouTube is also a good for general info - https://www.youtube.com/results?search_query=boolean+logic
-
@d-healey Thanks a lot!
-
HiseSnippet 1155.3ocsW1uahaDD.eMI9zg6Gpmz8.rG+EQIkCx2mRSaBjjJT9BURipTzoSarWBqhYWqcWmFTUj56SeI5iTeCZmwFBlD7o.zCIDL67g+Mi2c73VZkO2XTZhSwK5GwINesa69Ra2FcYBIo4A3B6aLhajmE2yPp2OhYL7.hiyB+LZgSwEIIe9mepNKjI84iVhPtTI74mH5IriVs0dGKBCOhEvuPzKi0quWSekrgJTECzrfaURDy+V1M7yXnYEbINu5v.gUoaaYVtg3rXcUP+1cU+tL09KEFw0gbTnFoMDnzkOREFfDiqRZzUDFzZXVaHPTZMpFrPZM3stmJBDOt9nZw2knfNxir0CmBii2BigWsr3UMCdS.ImLHsXJRuwssuVDYGoA44qbaJsbcGFT1yhRpsjB+0BtMTfERakdra4GoAgG8nbs0ptBcsUqtzNdddPs2Xo2wzznvXCcW5PGugaan5EojfP4RnxRfC3uULnJoUqBavBCuFtcUVEgYLFwQATF2qtUBg7pOtiWGklVV.BU2gJn+.sF96xKuj2e3UrXpkUhhMcKO4qOXQI5xTwRvkXn8WI9XE5DXAzN.lGvLzySHCERNsSrz2JTR5.CJ6OL9q.7FFyW58u21UXnQLMr2CJWFJSC9An2X+SNo99MN9cui1b7nYn2vkbMbw6S8YRZWXaGMPzoCWCQlBATdioBjldEwhhPFvuGJCCR4Dwy6LBEL+7JJ5TNkHuhX8AxPiJjWIRKfhQhOncO.lheeZ9kdy34oGVqyD4jP+jXWZ4RIU3Gf.+r3xBBvhasUP5WMIbnHdq5RLliHCuNzxIaVdT4Rzc2kVahYTo1bXaSPJlkFlZXvWchAOT4yBol3doExZvNCz1DciGYvFbefm2nhi7LkketrbB+fJ5SU0oyD0MXaVHWOQ0XWM8myQrzcMWm8lAZHbTe79Gt42+Ha6M+zyIYLTIaJE1yi3x7Z5QFb3B92u17.lkgMcFrFXWDWaEHBNGvuC5fm1Bpn6AbysVUDz24wSjDmWaSz9sCZPUO1ZURh.5C8JWHSqRtO6SD5mUvvti2T1RygCuXaKg4TUOHnLMXlSAtjA4K1w8DNSKQKr76sCaa9hQnVtH7i6MLf0lAXlBDVMOD9yi2a1qBqNMUg0xsJbwbfvZSCBqmaU3p4.g0mFD1HWDXyABaLMHrYdHrGeNPXyoAgsxcufXNPXqoAgsyCg+NbNPX6oAgOj6Mhn4.gO7hQ.erXdHP0yABKCSY+rIHgYWUAwgL63CzhSwOPA7TpwlhDmTTZD19Ymx++sobeoH9F2VBqe2IyXgIvH7rruDLN3cC9F2CgQ57si.bQ2i9suLuH.4WTwVXrwSYVs.1n3BuCVa30i74vUWJ4g3TBNEvmmmJWEkwJPatLHQ3egOCTVCkcFnr1PkjdLes5S9oylfu8wqSVAXRl7hWEcOEko0HIyq.94VsRUROXi2m78wz+6gQLlrOqNC9r1L3y5yfOaLC9r4L3yVyfOa+Y8AOyuerU0K83.rPqCSFhyw4vjlBI6BI+GfQeWT2
@d-healey @ustk Ehehe, I think Really tried my best. Probably the challenge is too big for me. This is how far I got and I don't really know how to break the problem into a smaller one..So I'm stuck.
-
@sawer Add a label to display the output and add an = button that when pressed carries out the calculation.
-
@d-healey Thanks.
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
-
@sawer said in Calculator Project:
How did you come up with he appendInput function , especially the "+="
+= Is a standard js operator
@sawer said in Calculator Project:
What's eval? There's no this such as that in the Api.
-
@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.