kingkapd
kingkapd

Reputation: 69

Simple Javascript Calculator, something went wrong?

I tried to make this simple JS calculator following a tutorial I found but it doesn't seem to be working at all. When I press one of the buttons nothing happens. I probably made a really stupid mistake so I'm sorry if I'm wasting your time. I've troubleshooted it but I can't find a solution.

Correction: It works now!

var a,b,result;
    function setValues()
    {
    	a = Number(document.getElementById("a").value);
    	b = Number(document.getElementById("b").value);
    }
    
    function sum()
    {
    	setValues();
    	result = a+b;
    	alert("The sum is equal to"+result);
    }
    
    function rest()
    {
    	setValues();
    	result = a-b;
    	alert("The rest is equal to"+result);
    }
    
    function mult()
    {
    	setValues();
    	result = a*b;
    	alert("The mult is equal to"+result);
    }
    
    function div()
    {
    	setValues();
    	result = a/b;
    	alert("The div is equal to"+result);
    }
<div>
  <input id="a" type="text">
  <input id="b" type="text">
  <input type="button" onclick="sum()" value="sum"/>
  <input type="button" onclick="rest()" value="rest"/>
  <input type="button" onclick="mult()" value="multiply"/>
  <input type="button" onclick="div()" value="divide"/>

</div>

Upvotes: 0

Views: 265

Answers (2)

Thomas Junk
Thomas Junk

Reputation: 5676

Typo?

a = Number(document.getElementByID("a").value);
b = Number(document.getElementByID("b").value);

Try:

a = Number(document.getElementById("a").value);
b = Number(document.getElementById("b").value);

Upvotes: 0

Justinas
Justinas

Reputation: 43441

You can check that your console (F12) is throwing error.

Change

document.getElementByID("a").value

to

document.getElementById("a").value

JavaScript is Case-Sensitive. So that's why js does not find getElementByID

Upvotes: 1

Related Questions