Нужна помощь с калькулятором Java Script
При делении на ноль результат выдает "Infinity". Как сделать так, чтобы вместо Infinity, в окошке писалось "На ноль делить нельзя!", либо выскакивал alert с данным сообщением? Вот код калькулятора:
<script> var Fcalc = document.calc; var Currents = 0; var FlagNewNum = false; var PendingOp = ""; function NumPressed (Num) { if (FlagNewNum) { Fcalc.Result.value = Num; FlagNewNum = false; } else { if (Fcalc.Result.value == "0") Fcalc.Result.value = Num; else Fcalc.Result.value += Num; } } function Operation (Op) { var Result = Fcalc.Result.value; if (FlagNewNum && PendingOp != "=") { Fcalc.Result.value = Currents; } else { FlagNewNum = true; if ( '+' == PendingOp ) Currents += parseFloat(Result); else if ( '-' == PendingOp ) Currents -= parseFloat(Result); else if ( '/' == PendingOp ) Currents /= parseFloat(Result); else if ( '*' == PendingOp ) Currents *= parseFloat(Result); else Currents = parseFloat(Result); Fcalc.Result.value = Currents; PendingOp = Op; } } function Point () { var curResult = Fcalc.Result.value; if (FlagNewNum) { curResult = "0."; FlagNewNum = false; } else { if (curResult.indexOf(".") == -1) curResult += "."; } Fcalc.Result.value = curResult; } function Clear () { Fcalc.Result.value = "0"; FlagNewNum = true; } function Neg () { Fcalc.Result.value = parseFloat(Fcalc.Result.value) * -1; } </script> <form name="calc" class="calculator-form" action=""> <table class="calculator"> <tr> <td colspan="5"> <input class="res-window" name="Result" style="height: 25px;" type="text" size="39" value="0"> </td> </tr> <tr> <td><input class="little-button" type="button" value="7" onclick="NumPressed(7)"/></td> <td><input class="little-button" type="button" value="8" onclick="NumPressed(8)"/></td> <td><input class="little-button" type="button" value="9" onclick="NumPressed(9)"/></td> <td><input class="little-button" type="button" value="/" onclick="Operation('/')"/></td> <td><input class="little-button" type="button" value="C" onclick="Clear()"/></td> </tr> <tr> <td><input class="little-button" type="button" value="4" onclick="NumPressed(4)"/></td> <td><input class="little-button" type="button" value="5" onclick="NumPressed(5)"/></td> <td><input class="little-button" type="button" value="6" onclick="NumPressed(6)"/></td> <td><input class="little-button" type="button" value="*" onclick="Operation('*')"/></td> <td><input class="little-button" type="button" value="±" onclick="Neg()"/></td> </tr> <tr> <td><input class="little-button" type="button" value="1" onclick="NumPressed(1)"/></td> <td><input class="little-button" type="button" value="2" onclick="NumPressed(2)"/></td> <td><input class="little-button" type="button" value="3" onclick="NumPressed(3)"/></td> <td><input class="little-button" type="button" value="-" onclick="Operation('-')"/></td> <td rowspan="2"><input class="res-button" type="button" value="=" onclick="Operation('=')"/></td> </tr> <tr> <td colspan="2"><input class="zero-button" type="button" value="0" onclick="NumPressed(0)"/></td> <td><input class="little-button" type="button" value="." onclick="Point()"/></td> <td><input class="little-button" type="button" value="+" onclick="Operation('+')"/></td> </tr> </table> </form> |
Andrehrist,
Найди это /*39 строка*/ Currents /= parseFloat(Result); и сделай проверку на Infinity через if-else |
Часовой пояс GMT +3, время: 02:50. |