JavaScript计算器警报不要除以0

时间:2016-10-16 13:53:45

标签: javascript calculator

我必须警惕不要除以0,怎么做

<!doctype html> 
<html lang="pl"> 
<head> 
<link rel="Stylesheet" type="text/css" href="style1.css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ZAD 5 - Kalkulator</title>
<meta name="owner" content="Adam Mika" />
<meta name="description" content="dskjadhjkhgkjdgkjgdkasgds" />
</head>
<body>
<div id="lewa">
MENU:<ul>
<li><a href="zad1.html">Zadanie 1</a></li>
<li><a href="zad2.html">Zadanie 2</a></li>
<li><a href="zad3.html">Zadanie 3</a></li>
<li><a href="zad4.html">Zadanie 4</a></li>
<li><a href="zad5.html">Zadanie 5</a></li>
</ul>
</div>
<div id="prawa">
ZADANIE 5 - Kalkulator
<br></br>
        <form name="kalk">
            <table align="center">
                <tr>
                    <td >
                        <input name="txtEkran" type="text" value=0 readonly />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value=7 onclick="cyfraClick(7)" />
                        <input type="button" value=8 onclick="cyfraClick(8)" />
                        <input type="button" value=9 onclick="cyfraClick(9)" />
                        <input type="button" value="/" onclick="operatClick('/')" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value=4 onclick="cyfraClick(4)" />
                        <input type="button" value=5 onclick="cyfraClick(5)" />
                        <input type="button" value=6 onclick="cyfraClick(6)" />
                        <input type="button" value="*" onclick="operatClick('*')" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value=1 onclick="cyfraClick(1)" />
                        <input type="button" value=2 onclick="cyfraClick(2)" />
                        <input type="button" value=3 onclick="cyfraClick(3)" />
                        <input type="button" value="-" onclick="operatClick('-')" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value=0 onclick="cyfraClick(0)" />
                        <input type="button" value="=" onclick="rownaClick()" />
                        <input type="button" value="+" onclick="operatClick('+')" />
                        <input type="button" value="CE" onclick="kasuj('CE')" />
                    </td>
                </tr>
            </table>
        </form>
        </div>

        <script src="zad5.js">
        </script>
      </body>
   </html>

JS CODE

var formKalk = window.document.kalk;
            var operat;
            var cyfraPierwsza = true;
            var walor1 = 0, walor2 = 0;

            function cyfraClick(cyfra){
                if (cyfraPierwsza || formKalk.txtEkran.value == 0){
                    formKalk.txtEkran.value  = cyfra;
                    cyfraPierwsza = false;
                }else{
                    formKalk.txtEkran.value += cyfra;
                }
            }

            function operatClick(opr){
                var txtEkran = formKalk.txtEkran.value;
                walor1 = parseFloat(txtEkran);
                operat = opr;
                cyfraPierwsza = true;
            }

            function rownaClick(){
                var rezultat;
                var txtEkran = formKalk.txtEkran.value;
                walor2 = parseFloat(txtEkran);
                if(operat == "/") {
                rezultat = walor1 / walor2;
                }else if(operat == "*") {
                    rezultat = walor1 * walor2;
                }else if(operat == "-"){
                    rezultat = walor1 - walor2;
                }
                else {
                    rezultat = walor1 + walor2;
                }

                if(rezultat == Infinity) rezultat = 0;




                formKalk.txtEkran.value = rezultat;
                walor1 = rezultat;
                cyfraPierwsza = true;
            }


            function kasuj(c){
                formKalk.txtEkran.value = 0;
                cyfraPierwsza = true;

                if (c == "C"){
                    walor1 = 0;
                    operat = "";
                }
            }

您认为是什么导致它?

1 个答案:

答案 0 :(得分:1)

这是一个相当简单的解决方案 - 在分割之前检查walor1的值是否为零。如果是,请告诉用户不要除以零,然后退出该功能。否则,执行除法。

if(operat == "/") {
   if (walor2 == 0) {
      alert("Please don't divide by zero.");
      return;
   }
   rezultat = walor1 / walor2;
}