Kamil Czaderna Kamil Czaderna - 23 days ago 9
Javascript Question

JavaScript Calculator alert dont divide by 0

i have to do alert about dont divide by 0, how do it

<!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 = "";
}
}





What do you think is causing it?

Answer

It's a rather simple solution - check to see if walor1 has the value of zero before dividing. If so, tell the user to not divide by zero, and exit the function. Otherwise, perform the division.

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