I am looking for any errors in my JavaScript Calculator, please:
THanks!
- Bruce
I am looking for any errors in my JavaScript Calculator, please:
THanks!
15000 + 020 gives 15016
I like the style!
000.00.3X9 = ???
This result goes outside the view area: 8.017420608e-12
Minus 9 minus 6 = 3.
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.
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.
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?
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.
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
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
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: