<form name="frm"> <table border="1"> <tr> <td colspan="5" id="res"><input name="ReadOut" type="Text"><br /></td> </tr> <tr> <td><button type="button" name="7" value="7" onClick="NumPressed(7)">7</button></td> <td><button type="button" name="8" value="8" onClick="NumPressed(8)">8</button></td> <td><button type="button" name="9" value="9" onClick="NumPressed(9)">9</button></td> <td><button type="button" name="/" value=" / " onclick="Operation('/')">/</button></td> <td><button type="button" name="c" value="C" onclick="Clear()">C</button></td> </tr> <tr> <td><button type="button" name="4" value="4" onClick="NumPressed(4)">4</button></td> <td><button type="button" name="5" value="5" onClick="NumPressed(5)">5</button></td> <td><button type="button" name="6" value="6" onClick="NumPressed(6)">6</button></td> <td><button type="button" name="*" value="*" onclick="Operation('*')">*</button></td> <td><button type="button" name="pm" onclick="Neg()">±</button></td> </tr> <tr> <td><button type="button" name="1" value="1" onClick="NumPressed(1)">1</button></td> <td><button type="button" name="2" value="2" onClick="NumPressed(2)">2</button></td> <td><button type="button" name="3" value="3" onClick="NumPressed(3)">3</button></td> <td><button type="button" name="-" value="-" onclick="Operation('-')">-</button></td> <td rowspan="2"> <button type="button" id="f" name="=" value="=" onclick="Operation('=')">=</button><br /></td> </tr> <tr> <td colspan="2"><button type="button" name="0" value="0" onClick="NumPressed(0)">0</button></td> <td><button type="button" name="." value="." onclick="Decimal()">.</button></td> <td><button type="button" name="+" value="+" onclick="Operation('+')">+</button></td> </tr> </table> </form> <script> var Fcalc = document.frm; var Currents = 0; var FlagNewNum = false; var PendingOp = ""; function NumPressed (Num) { if (FlagNewNum) { Fcalc.ReadOut.value = Num; FlagNewNum = false; } else { if (Fcalc.ReadOut.value == "0") Fcalc.ReadOut.value = Num; else Fcalc.ReadOut.value += Num; } } function Operation (Op) { var Readout = Fcalc.ReadOut.value; if (FlagNewNum && PendingOp != "=") { Fcalc.ReadOut.value = Currents; } else { FlagNewNum = true; if ( '+' == PendingOp ) Currents += parseFloat(Readout); else if ( '-' == PendingOp ) Currents -= parseFloat(Readout); else if ( '/' == PendingOp ) Currents /= parseFloat(Readout); else if ( '*' == PendingOp ) Currents *= parseFloat(Readout); else Currents = parseFloat(Readout); Fcalc.ReadOut.value = Currents; PendingOp = Op; } } function Decimal () { var curReadOut = Fcalc.ReadOut.value; if (FlagNewNum) { curReadOut = "0."; FlagNewNum = false; } else { if (curReadOut.indexOf(".") == -1) curReadOut += "."; } Fcalc.ReadOut.value = curReadOut; } function Clear () { Currents = 0; PendingOp = ""; Fcalc.ReadOut.value = "0"; FlagNewNum = true; } function Neg () { Fcalc.ReadOut.value = parseFloat(Fcalc.ReadOut.value) * -1; } </script>
|
1 answer
For your code already offered an answer
function calculate() { var eval1 = eval(document.frm.result.value); if(Math.abs(eval1) == Infinity) alert('Деление на ноль!'); else document.frm.result.value = eval1; }
But it is too simple and not interesting, and besides, an extra operation is performed, it is better to check before performing a great eval
, let's think about what we can do
function calculate() { var mathString = document.frm.result.value; var regexp = new RegExp(/\d*\/0/); // строка вида число/0 var isZeroDivision= regexp.test(mathString); if(isZeroDivision) return alert("Деление на ноль"); /** ваш код из ф-ции calculate */ }
Even more perverted option in your style.
function calculate() { var mathString = document.frm.result.value; var len = mathString.len; var lastNumber = mathString[len-1]; // последние элемент if(lastNumber == '0') { /* если последняя цифра 0, уже стоит задуматься, а не пытаемся ли мы на него поделить */ if(mathString[len-2] == '/') return alert("Деление на ноль"); } /** ваш код из ф-ции calculate */ }
ZY
In general, since calculators are not worth writing, and eval
generally bjaka
- And also, the code in question has changed - Grundy
- @Grundy, Lord, did I write so long the answer?: D - ThisMan
- there were some minor misunderstandings with editing, the author changed the code, then accepted the edit to the old code, and then rolled it back :) - Grundy
- Thank you! Earned! - Anton Evseev
|
Infinity
- Then write an error, for example - Grundy