javaScript calculator when dividing by 0












-1


















<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>

<html>
<head>
<body>
<br><br>

<div class="box">
<div class="display"><input type="text" readonly size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value="(" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>





When I'm dividing any number by 0 it says "infinity" but I want this to show result by displaying "NaN" rather than infinity. I tried but I couldn't figure it out.






<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>
</head>
<body>
<br><br>
<center><h1>JavaScript Calculator</h1></center>
<h2><center><a href="index.html">Scientific
Calculator</a></center></h2>
<div class="box">
<div class="display"><input type="text" readonly
size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value=" (" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>


Thanks in advance










share|improve this question

























  • You could add a check to the result if(!isFinite()) return NaN

    – Steve
    Nov 20 '18 at 7:52
















-1


















<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>

<html>
<head>
<body>
<br><br>

<div class="box">
<div class="display"><input type="text" readonly size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value="(" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>





When I'm dividing any number by 0 it says "infinity" but I want this to show result by displaying "NaN" rather than infinity. I tried but I couldn't figure it out.






<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>
</head>
<body>
<br><br>
<center><h1>JavaScript Calculator</h1></center>
<h2><center><a href="index.html">Scientific
Calculator</a></center></h2>
<div class="box">
<div class="display"><input type="text" readonly
size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value=" (" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>


Thanks in advance










share|improve this question

























  • You could add a check to the result if(!isFinite()) return NaN

    – Steve
    Nov 20 '18 at 7:52














-1












-1








-1











<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>

<html>
<head>
<body>
<br><br>

<div class="box">
<div class="display"><input type="text" readonly size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value="(" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>





When I'm dividing any number by 0 it says "infinity" but I want this to show result by displaying "NaN" rather than infinity. I tried but I couldn't figure it out.






<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>
</head>
<body>
<br><br>
<center><h1>JavaScript Calculator</h1></center>
<h2><center><a href="index.html">Scientific
Calculator</a></center></h2>
<div class="box">
<div class="display"><input type="text" readonly
size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value=" (" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>


Thanks in advance










share|improve this question



















<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>

<html>
<head>
<body>
<br><br>

<div class="box">
<div class="display"><input type="text" readonly size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value="(" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>





When I'm dividing any number by 0 it says "infinity" but I want this to show result by displaying "NaN" rather than infinity. I tried but I couldn't figure it out.






<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>
</head>
<body>
<br><br>
<center><h1>JavaScript Calculator</h1></center>
<h2><center><a href="index.html">Scientific
Calculator</a></center></h2>
<div class="box">
<div class="display"><input type="text" readonly
size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value=" (" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>


Thanks in advance






<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>

<html>
<head>
<body>
<br><br>

<div class="box">
<div class="display"><input type="text" readonly size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value="(" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>





<script>
function c(val)
{
document.getElementById("d").value=val;
}
function math(val)
{
document.getElementById("d").value+=val;
}
function e()
{
try
{
c(eval(document.getElementById("d").value))
}
catch(e)
{
c('Error')
}
}


</script>

<html>
<head>
<body>
<br><br>

<div class="box">
<div class="display"><input type="text" readonly size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value="(" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>

</body>
</html>






javascript html css






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 7:52









Snake Eyes

8,3002683157




8,3002683157










asked Nov 20 '18 at 7:50









ninganinga

216




216













  • You could add a check to the result if(!isFinite()) return NaN

    – Steve
    Nov 20 '18 at 7:52



















  • You could add a check to the result if(!isFinite()) return NaN

    – Steve
    Nov 20 '18 at 7:52

















You could add a check to the result if(!isFinite()) return NaN

– Steve
Nov 20 '18 at 7:52





You could add a check to the result if(!isFinite()) return NaN

– Steve
Nov 20 '18 at 7:52












2 Answers
2






active

oldest

votes


















0














Use the conditional operator in your c function - if val is Infinity (or -Infinity, I suppose), display NaN instead, otherwise display val:






function c(val) {
document.getElementById("d").value = (val === Infinity || val === -Infinity) ? 'NaN' : val;
}

function math(val) {
document.getElementById("d").value += val;
}

function e() {
try {
c(eval(document.getElementById("d").value))
} catch (e) {
c('Error')
}
}

<br><br>
<center>
<h1>JavaScript Calculator</h1>
</center>
<h2>
<center><a href="index.html">Scientific
Calculator</a></center>
</h2>
<div class="box">
<div class="display"><input type="text" readonly size="16" id="d"></div>
<div class="keys">

<p>
<input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
<input type="button" class="button gray" value=" (" onclick='math("(")'>
<input type="button" class="button gray" value=")" onclick='math(")")'>
<input type="button" class="button pink" value="/" onclick='math("/")'>
</p>
<p>
<input type="button" class="button red" value="7" onclick='math("7")'>
<input type="button" class="button red" value="8" onclick='math("8")'>
<input type="button" class="button red" value="9" onclick='math("9")'>
<input type="button" class="button pink" value="*" onclick='math("*")'>
</p>
<p>
<input type="button" class="button red" value="4" onclick='math("4")'>
<input type="button" class="button red" value="5" onclick='math("5")'>
<input type="button" class="button red" value="6" onclick='math("6")'>
<input type="button" class="button pink" value="-" onclick='math("-")'>
</p>
<p>
<input type="button" class="button red" value="1" onclick='math("1")'>
<input type="button" class="button red" value="2" onclick='math("2")'>
<input type="button" class="button red" value="3" onclick='math("3")'>
<input type="button" class="button pink" value="+" onclick='math("+")'>
</p>
<p>
<input type="button" class="button red" value="0" onclick='math("0")'>
<input type="button" class="button red" value="." onclick='math(".")'>
<input type="button" class="button red" value="C" onclick='c("")'>
<input type="button" class="button orange" value="=" onclick='e()'>
</p>
</div>
</div>








share|improve this answer































    0














    you can add a check in your functin c() like this



    function c(val)
    {
    if ( val == Infinity || val == -Infinity )
    val = NaN;

    document.getElementById("d").value=val;

    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388429%2fjavascript-calculator-when-dividing-by-0%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Use the conditional operator in your c function - if val is Infinity (or -Infinity, I suppose), display NaN instead, otherwise display val:






      function c(val) {
      document.getElementById("d").value = (val === Infinity || val === -Infinity) ? 'NaN' : val;
      }

      function math(val) {
      document.getElementById("d").value += val;
      }

      function e() {
      try {
      c(eval(document.getElementById("d").value))
      } catch (e) {
      c('Error')
      }
      }

      <br><br>
      <center>
      <h1>JavaScript Calculator</h1>
      </center>
      <h2>
      <center><a href="index.html">Scientific
      Calculator</a></center>
      </h2>
      <div class="box">
      <div class="display"><input type="text" readonly size="16" id="d"></div>
      <div class="keys">

      <p>
      <input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
      <input type="button" class="button gray" value=" (" onclick='math("(")'>
      <input type="button" class="button gray" value=")" onclick='math(")")'>
      <input type="button" class="button pink" value="/" onclick='math("/")'>
      </p>
      <p>
      <input type="button" class="button red" value="7" onclick='math("7")'>
      <input type="button" class="button red" value="8" onclick='math("8")'>
      <input type="button" class="button red" value="9" onclick='math("9")'>
      <input type="button" class="button pink" value="*" onclick='math("*")'>
      </p>
      <p>
      <input type="button" class="button red" value="4" onclick='math("4")'>
      <input type="button" class="button red" value="5" onclick='math("5")'>
      <input type="button" class="button red" value="6" onclick='math("6")'>
      <input type="button" class="button pink" value="-" onclick='math("-")'>
      </p>
      <p>
      <input type="button" class="button red" value="1" onclick='math("1")'>
      <input type="button" class="button red" value="2" onclick='math("2")'>
      <input type="button" class="button red" value="3" onclick='math("3")'>
      <input type="button" class="button pink" value="+" onclick='math("+")'>
      </p>
      <p>
      <input type="button" class="button red" value="0" onclick='math("0")'>
      <input type="button" class="button red" value="." onclick='math(".")'>
      <input type="button" class="button red" value="C" onclick='c("")'>
      <input type="button" class="button orange" value="=" onclick='e()'>
      </p>
      </div>
      </div>








      share|improve this answer




























        0














        Use the conditional operator in your c function - if val is Infinity (or -Infinity, I suppose), display NaN instead, otherwise display val:






        function c(val) {
        document.getElementById("d").value = (val === Infinity || val === -Infinity) ? 'NaN' : val;
        }

        function math(val) {
        document.getElementById("d").value += val;
        }

        function e() {
        try {
        c(eval(document.getElementById("d").value))
        } catch (e) {
        c('Error')
        }
        }

        <br><br>
        <center>
        <h1>JavaScript Calculator</h1>
        </center>
        <h2>
        <center><a href="index.html">Scientific
        Calculator</a></center>
        </h2>
        <div class="box">
        <div class="display"><input type="text" readonly size="16" id="d"></div>
        <div class="keys">

        <p>
        <input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
        <input type="button" class="button gray" value=" (" onclick='math("(")'>
        <input type="button" class="button gray" value=")" onclick='math(")")'>
        <input type="button" class="button pink" value="/" onclick='math("/")'>
        </p>
        <p>
        <input type="button" class="button red" value="7" onclick='math("7")'>
        <input type="button" class="button red" value="8" onclick='math("8")'>
        <input type="button" class="button red" value="9" onclick='math("9")'>
        <input type="button" class="button pink" value="*" onclick='math("*")'>
        </p>
        <p>
        <input type="button" class="button red" value="4" onclick='math("4")'>
        <input type="button" class="button red" value="5" onclick='math("5")'>
        <input type="button" class="button red" value="6" onclick='math("6")'>
        <input type="button" class="button pink" value="-" onclick='math("-")'>
        </p>
        <p>
        <input type="button" class="button red" value="1" onclick='math("1")'>
        <input type="button" class="button red" value="2" onclick='math("2")'>
        <input type="button" class="button red" value="3" onclick='math("3")'>
        <input type="button" class="button pink" value="+" onclick='math("+")'>
        </p>
        <p>
        <input type="button" class="button red" value="0" onclick='math("0")'>
        <input type="button" class="button red" value="." onclick='math(".")'>
        <input type="button" class="button red" value="C" onclick='c("")'>
        <input type="button" class="button orange" value="=" onclick='e()'>
        </p>
        </div>
        </div>








        share|improve this answer


























          0












          0








          0







          Use the conditional operator in your c function - if val is Infinity (or -Infinity, I suppose), display NaN instead, otherwise display val:






          function c(val) {
          document.getElementById("d").value = (val === Infinity || val === -Infinity) ? 'NaN' : val;
          }

          function math(val) {
          document.getElementById("d").value += val;
          }

          function e() {
          try {
          c(eval(document.getElementById("d").value))
          } catch (e) {
          c('Error')
          }
          }

          <br><br>
          <center>
          <h1>JavaScript Calculator</h1>
          </center>
          <h2>
          <center><a href="index.html">Scientific
          Calculator</a></center>
          </h2>
          <div class="box">
          <div class="display"><input type="text" readonly size="16" id="d"></div>
          <div class="keys">

          <p>
          <input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
          <input type="button" class="button gray" value=" (" onclick='math("(")'>
          <input type="button" class="button gray" value=")" onclick='math(")")'>
          <input type="button" class="button pink" value="/" onclick='math("/")'>
          </p>
          <p>
          <input type="button" class="button red" value="7" onclick='math("7")'>
          <input type="button" class="button red" value="8" onclick='math("8")'>
          <input type="button" class="button red" value="9" onclick='math("9")'>
          <input type="button" class="button pink" value="*" onclick='math("*")'>
          </p>
          <p>
          <input type="button" class="button red" value="4" onclick='math("4")'>
          <input type="button" class="button red" value="5" onclick='math("5")'>
          <input type="button" class="button red" value="6" onclick='math("6")'>
          <input type="button" class="button pink" value="-" onclick='math("-")'>
          </p>
          <p>
          <input type="button" class="button red" value="1" onclick='math("1")'>
          <input type="button" class="button red" value="2" onclick='math("2")'>
          <input type="button" class="button red" value="3" onclick='math("3")'>
          <input type="button" class="button pink" value="+" onclick='math("+")'>
          </p>
          <p>
          <input type="button" class="button red" value="0" onclick='math("0")'>
          <input type="button" class="button red" value="." onclick='math(".")'>
          <input type="button" class="button red" value="C" onclick='c("")'>
          <input type="button" class="button orange" value="=" onclick='e()'>
          </p>
          </div>
          </div>








          share|improve this answer













          Use the conditional operator in your c function - if val is Infinity (or -Infinity, I suppose), display NaN instead, otherwise display val:






          function c(val) {
          document.getElementById("d").value = (val === Infinity || val === -Infinity) ? 'NaN' : val;
          }

          function math(val) {
          document.getElementById("d").value += val;
          }

          function e() {
          try {
          c(eval(document.getElementById("d").value))
          } catch (e) {
          c('Error')
          }
          }

          <br><br>
          <center>
          <h1>JavaScript Calculator</h1>
          </center>
          <h2>
          <center><a href="index.html">Scientific
          Calculator</a></center>
          </h2>
          <div class="box">
          <div class="display"><input type="text" readonly size="16" id="d"></div>
          <div class="keys">

          <p>
          <input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
          <input type="button" class="button gray" value=" (" onclick='math("(")'>
          <input type="button" class="button gray" value=")" onclick='math(")")'>
          <input type="button" class="button pink" value="/" onclick='math("/")'>
          </p>
          <p>
          <input type="button" class="button red" value="7" onclick='math("7")'>
          <input type="button" class="button red" value="8" onclick='math("8")'>
          <input type="button" class="button red" value="9" onclick='math("9")'>
          <input type="button" class="button pink" value="*" onclick='math("*")'>
          </p>
          <p>
          <input type="button" class="button red" value="4" onclick='math("4")'>
          <input type="button" class="button red" value="5" onclick='math("5")'>
          <input type="button" class="button red" value="6" onclick='math("6")'>
          <input type="button" class="button pink" value="-" onclick='math("-")'>
          </p>
          <p>
          <input type="button" class="button red" value="1" onclick='math("1")'>
          <input type="button" class="button red" value="2" onclick='math("2")'>
          <input type="button" class="button red" value="3" onclick='math("3")'>
          <input type="button" class="button pink" value="+" onclick='math("+")'>
          </p>
          <p>
          <input type="button" class="button red" value="0" onclick='math("0")'>
          <input type="button" class="button red" value="." onclick='math(".")'>
          <input type="button" class="button red" value="C" onclick='c("")'>
          <input type="button" class="button orange" value="=" onclick='e()'>
          </p>
          </div>
          </div>








          function c(val) {
          document.getElementById("d").value = (val === Infinity || val === -Infinity) ? 'NaN' : val;
          }

          function math(val) {
          document.getElementById("d").value += val;
          }

          function e() {
          try {
          c(eval(document.getElementById("d").value))
          } catch (e) {
          c('Error')
          }
          }

          <br><br>
          <center>
          <h1>JavaScript Calculator</h1>
          </center>
          <h2>
          <center><a href="index.html">Scientific
          Calculator</a></center>
          </h2>
          <div class="box">
          <div class="display"><input type="text" readonly size="16" id="d"></div>
          <div class="keys">

          <p>
          <input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
          <input type="button" class="button gray" value=" (" onclick='math("(")'>
          <input type="button" class="button gray" value=")" onclick='math(")")'>
          <input type="button" class="button pink" value="/" onclick='math("/")'>
          </p>
          <p>
          <input type="button" class="button red" value="7" onclick='math("7")'>
          <input type="button" class="button red" value="8" onclick='math("8")'>
          <input type="button" class="button red" value="9" onclick='math("9")'>
          <input type="button" class="button pink" value="*" onclick='math("*")'>
          </p>
          <p>
          <input type="button" class="button red" value="4" onclick='math("4")'>
          <input type="button" class="button red" value="5" onclick='math("5")'>
          <input type="button" class="button red" value="6" onclick='math("6")'>
          <input type="button" class="button pink" value="-" onclick='math("-")'>
          </p>
          <p>
          <input type="button" class="button red" value="1" onclick='math("1")'>
          <input type="button" class="button red" value="2" onclick='math("2")'>
          <input type="button" class="button red" value="3" onclick='math("3")'>
          <input type="button" class="button pink" value="+" onclick='math("+")'>
          </p>
          <p>
          <input type="button" class="button red" value="0" onclick='math("0")'>
          <input type="button" class="button red" value="." onclick='math(".")'>
          <input type="button" class="button red" value="C" onclick='c("")'>
          <input type="button" class="button orange" value="=" onclick='e()'>
          </p>
          </div>
          </div>





          function c(val) {
          document.getElementById("d").value = (val === Infinity || val === -Infinity) ? 'NaN' : val;
          }

          function math(val) {
          document.getElementById("d").value += val;
          }

          function e() {
          try {
          c(eval(document.getElementById("d").value))
          } catch (e) {
          c('Error')
          }
          }

          <br><br>
          <center>
          <h1>JavaScript Calculator</h1>
          </center>
          <h2>
          <center><a href="index.html">Scientific
          Calculator</a></center>
          </h2>
          <div class="box">
          <div class="display"><input type="text" readonly size="16" id="d"></div>
          <div class="keys">

          <p>
          <input type="button" class="button gray" value="mrc" onclick='c("...Made by Shriyansh...")'>
          <input type="button" class="button gray" value=" (" onclick='math("(")'>
          <input type="button" class="button gray" value=")" onclick='math(")")'>
          <input type="button" class="button pink" value="/" onclick='math("/")'>
          </p>
          <p>
          <input type="button" class="button red" value="7" onclick='math("7")'>
          <input type="button" class="button red" value="8" onclick='math("8")'>
          <input type="button" class="button red" value="9" onclick='math("9")'>
          <input type="button" class="button pink" value="*" onclick='math("*")'>
          </p>
          <p>
          <input type="button" class="button red" value="4" onclick='math("4")'>
          <input type="button" class="button red" value="5" onclick='math("5")'>
          <input type="button" class="button red" value="6" onclick='math("6")'>
          <input type="button" class="button pink" value="-" onclick='math("-")'>
          </p>
          <p>
          <input type="button" class="button red" value="1" onclick='math("1")'>
          <input type="button" class="button red" value="2" onclick='math("2")'>
          <input type="button" class="button red" value="3" onclick='math("3")'>
          <input type="button" class="button pink" value="+" onclick='math("+")'>
          </p>
          <p>
          <input type="button" class="button red" value="0" onclick='math("0")'>
          <input type="button" class="button red" value="." onclick='math(".")'>
          <input type="button" class="button red" value="C" onclick='c("")'>
          <input type="button" class="button orange" value="=" onclick='e()'>
          </p>
          </div>
          </div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 7:51









          CertainPerformanceCertainPerformance

          87.9k154775




          87.9k154775

























              0














              you can add a check in your functin c() like this



              function c(val)
              {
              if ( val == Infinity || val == -Infinity )
              val = NaN;

              document.getElementById("d").value=val;

              }





              share|improve this answer




























                0














                you can add a check in your functin c() like this



                function c(val)
                {
                if ( val == Infinity || val == -Infinity )
                val = NaN;

                document.getElementById("d").value=val;

                }





                share|improve this answer


























                  0












                  0








                  0







                  you can add a check in your functin c() like this



                  function c(val)
                  {
                  if ( val == Infinity || val == -Infinity )
                  val = NaN;

                  document.getElementById("d").value=val;

                  }





                  share|improve this answer













                  you can add a check in your functin c() like this



                  function c(val)
                  {
                  if ( val == Infinity || val == -Infinity )
                  val = NaN;

                  document.getElementById("d").value=val;

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 7:59









                  Pratik RathiPratik Rathi

                  66




                  66






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388429%2fjavascript-calculator-when-dividing-by-0%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)