Please test my JavaScript Calculator

I am looking for any errors in my JavaScript Calculator, please:

THanks!

  • Bruce
1 Like

15000 + 020 gives 15016

1 Like

I like the style! :smiley:
000.00.3X9 = ???
This result goes outside the view area: 8.017420608e-12
Minus 9 minus 6 = 3.

1 Like

15000 + 020 gives 15016

Thatā€™s really odd! Iā€™ll have to figure that out. Thanks.

Quick hintā€¦ Numbers starting with zero are parsed as base 8 numbers, not as decimal.

Itā€™s not that easy to click on the zero button as your div has a width of 210% but its parent td is only half of that, making only half of the button clickable.

2 Likes

000.00.3X9 = ???

Thatā€™s one I hadnā€™t thought of, two decimal points separated by digits. It registers nothing, I think, because eval() doesnā€™t know what to do with it. Iā€™ll have to fix that.

This result goes outside the view area: 8.017420608e-12

Iā€™m not sure what your input was exactly but I designed the bottom line to keep accepting input and allowing you to see the last 22 characters.

Minus 9 minus 6 = 3.

I didnā€™t allow for entering a negative number as the first entry because the example in the challenge didnā€™t but I would like to add that.

Thanks for your input Johnny!

Itā€™s not that easy to click on the zero button

I noticed that. Iā€™ll try to fix it.

I> Numbers starting with zero are parsed as base 8 numbers, not as decimal

I think I need to try to come up with one regex expression to filter out most or all of the data-entry errors, as opposed to the various conditional statements Iā€™m currently using for a lot of them.

sorry to derail but I would love feedback for my Wikipedia viewer

It looks pretty good! The only thing I would suggest adding is the ability to search by hitting the enter key while the cursor is still in the search box.

I did also get duplicate results on my first try but I wasnā€™t able to recreate it.

1 Like

humā€¦ Eval? ok, Iā€™m a Crockford fanboy, but this article isnā€™t https://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/

Well, I donā€™t know how else I would do math with a variable and though user input is involved, Iā€™ve restricted the allowable characters to prevent code injection.

The speed doesnā€™t seem to have been significantly affected either.

parseInt? :smirk:
If you want to learn about code injection that is cool, but pls donā€™t forget the code injection context ā€¦

Are you saying that despite severely limiting the characters that can be passed to eval(), code injection is still possible?

Oh plsā€¦ code injectio in a calculator machine? that is what Iā€™m saying.
By the way Iā€™m about to finish the calc with no Eval within, your problem isnā€™t to convert isnā€™t to parseInt (actually parseFloat) is to extract the operators but you will also find what I am using here:

In defence of @mrthnmn itā€™s not that easy to understand what youā€™re actually trying to say. :slight_smile:

1 Like

Iā€™m saying that you donā€™t need Eval in this case in particular, you can use the stackoverflow suggestion to avoid it, and there are more ways to do it than this example.
If you tell me that you prefer Eval to prevent code injection, unless you have a very particular case its very unlikely that a hacker will try to inject code in a Calculator machine, why would he/she?
If there is something less clear, pls ask, Iā€™m a non-native English speaker ā€¦ it isnā€™t unlikely that some of my statements arenā€™t exactly what I intended to say.
Bast regards,

I understand now. I donā€™t think thereā€™s much chance of someone hacking this page either. I canā€™t even imagine any damage could be done, beyond the page itself. But since Iā€™m eventually going to put this on my own web site I donā€™t want to casually dismiss any suggestion that Iā€™m wrong about that.

Iā€™ll be interested to see your version using a function for each operator. I looked into using parseInt (and parseFloat) at your suggestion and I imagined difficulty and complexity where I could safely use eval and didnā€™t pursue it. Thanks for your input though.

My project is missing validation for the size of the strings/values ā€¦ and Iā€™m about to give up on using keyPress to also use the keyboard to insert calculations.
In 2 or 3 days it will be done, but the code I am using and working fine is below, the numbers are already inserted in a global array that never as more than 2 elements, the previous total and the inserted value:

function convertNumbersOperators() {
var operatorsChange = {
ā€™+ā€™: function (a, b) { return a + b },
ā€™-ā€™: function (a, b) { return a - b },
ā€˜Ć·ā€™: function (a, b) { return a / b },
ā€˜xā€™: function (a, b) { return a * b }
};
var total = operatorsChange[operators[0]](numbersInserted[0], numbersInserted[1]);
document.getElementById(ā€˜answerā€™).innerHTML = total;
if (Number.isFinite(total)) {
numbersInserted = [total];
operators = [operators[1]];
} else {
document.getElementById(ā€˜historyā€™).innerHTML = ā€œā€;
clearOnIns = true;
numbersInserted = [0];
operators = ["+"];
}
}

It was quiet easy to do actualy ā€¦ but I also work with js for some time (14 years) and some things that for me are now vary obvious may not be that clear for everyone :wink:

Here, just migrated from VS2015 Community to codePen.
Not finished as previously told, but after checking some calculators around in the forum I will use some time to attempt the keyPress :wink:

Note: I also have more than enough experience with VS and for professional reasons it is good to keep a look at what MS is doing, also because they seem to be very encouraging of Open Source in the last few years (donā€™t remind me of their more distant past, lol)

So I think Iā€™ve fixed all the errors. Once again, it would be a huge help to know about any I might have missed:

  • Bruce