Please test my JavaScript Calculator

Please test my JavaScript Calculator
0.0 0

#1

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

THanks!

  • Bruce

#2

15000 + 020 gives 15016


#3

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


#4

15000 + 020 gives 15016

That’s really odd! I’ll have to figure that out. Thanks.


#5

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.


#6

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!


#7

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.


#8

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


#9

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.


#10

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/


#11

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.


#12

parseInt? :smirk:
If you want to learn about code injection that is cool, but pls don’t forget the code injection context …


#13

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


#14

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:


#15

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


#16

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,


#17

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.


#18

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:


#19

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)


#20

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