0

I don't know what is the problem in this code.

There are two input fields which are used for two variables to add, subtract, multiply and divide and 4 buttons (one each for add, subtract, multiply, and divide), but when I click on them nothing happens.

<h1>Calculator</h1>
<input id="f1" />
<input id="f2" />
<button id="add">Add</button>
<button id="sub">Sub</button>
<button id="multi">Multi</button>
<button id="div">Div</button>
var f1, f2, result;
document.getElementById("add").onclick=function{
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = return f1+f2;
}
document.getElementById("sub").onclick=function{
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = return f1-f2;
}
document.getElementById("multi").onclick=function{
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = return f1*f2;
}
document.getElementById("div").onclick=function{
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = return f1/f2;
}
if(result!=null)
alert("Result = "+result);
4
  • Check your syntax for errors here jshint.com Commented Oct 20, 2014 at 4:25
  • their is no method name you have given and not called on button click at all...then how do u expect to something happen on button click? Commented Oct 20, 2014 at 4:27
  • @SHEKHARSHETE it is not necessary to give method name, i have given id instead so function will be called when the button related to that ID is clicked... Commented Oct 21, 2014 at 5:39
  • YES I AGREE! :) your syntax of calling function is incorrect! function{} must be replaced with function(){} right... Commented Oct 21, 2014 at 5:42

4 Answers 4

2

You have some mistakes and syntax errors:

  1. The function need ():

    onclick = function () {}
    
  2. There is no need to use return in this case, just store the result into your variable:

    result = f1+f2;
    
  3. You should convert your textbox values into numbers:

    f1 = document.getElementById("f1").value * 1;
    
  4. You should show the result in each onclick, not at the end your script.

var f1, f2, result;
document.getElementById("add").onclick=function(){
	f1= document.getElementById("f1").value * 1;
	f2= document.getElementById("f2").value * 1;
	result =  f1+f2;
	alert("Result = "+result);
}
document.getElementById("sub").onclick=function(){
	f1= document.getElementById("f1").value * 1;
	f2= document.getElementById("f2").value * 1;
	result =  f1-f2;
	alert("Result = "+result);
}
document.getElementById("multi").onclick=function(){
	f1= document.getElementById("f1").value * 1;
	f2= document.getElementById("f2").value * 1;
	result =  f1*f2;
	alert("Result = "+result);
}
document.getElementById("div").onclick=function(){
	f1= document.getElementById("f1").value * 1;
	f2= document.getElementById("f2").value * 1;
	result =  f1/f2;
	alert("Result = "+result);
}
<h1>Calculator</h1>
<input id="f1" />
<input id="f2" />
<button id="add">Add</button>
<button id="sub">Sub</button>
<button id="multi">Multi</button>
<button id="div">Div</button>

Sign up to request clarification or add additional context in comments.

1 Comment

hey @ROX! nice to see the "Run Code" feature... :)
0

Please find the working code below

var f1, f2, result;
document.getElementById("add").onclick=function(){

f1= document.getElementById("f1").value;
f2= document.getElementById("f2").value;
result = f1+f2;

if(result!=null)
alert("Result = "+result);
}

Inorde to assign the value to a variable there is no need to use return keyword . Also as you attaching click event for each button seperatly in each of them you need code to display the result. Hope you can find & correct the other function as above.

Comments

0

you have some error in your code:
1- you are returning value but never use it.
2- your function definition need ()
3- yo should to convert input values to int
then this will work for you

var f1, f2, result;
document.getElementById("add").onclick=function(){
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = parseInt(f1)+parseInt(f2);
    if(result!=null)
        alert("Result = "+result);
}
document.getElementById("sub").onclick=function(){
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = parseInt(f1)-parseInt(f2);
    if(result!=null)
        alert("Result = "+result);
}
document.getElementById("multi").onclick=function(){
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = parseInt(f1)*parseInt(f2);
    if(result!=null)
        alert("Result = "+result);
}
document.getElementById("div").onclick=function(){
    f1= document.getElementById("f1").value;
    f2= document.getElementById("f2").value;
    result = parseInt(f1)/parseInt(f2);
    if(result!=null)
        alert("Result = "+result);
}

Comments

0
<!doctype html>
<html>
<head>
<title>Javascript</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />  
</head>
<body>
<h1>Calculator</h1>
<input id="f1" />
<input id="f2" />
<button id="add">Add</button>
<button id="sub">Sub</button>
<button id="multi">Multi</button>
<button id="div">Div</button>
<script type="text/javascript">
var f1, f2, result;
document.getElementById("add").onclick = function () {
    f1 = parseFloat(document.getElementById("f1").value);
    f2 = parseFloat(document.getElementById("f2").value);
    result = f1 + f2;
    showResult();
}
document.getElementById("sub").onclick = function () {
    f1 = parseFloat(document.getElementById("f1").value);
    f2 = parseFloat(document.getElementById("f2").value);
    result = f1 - f2;
    showResult();
}
document.getElementById("multi").onclick = function () {
    f1 = parseFloat(document.getElementById("f1").value);
    f2 = parseFloat(document.getElementById("f2").value);
    result = f1 * f2;
    showResult();
}
document.getElementById("div").onclick = function () {
    f1 = parseFloat(document.getElementById("f1").value);
    f2 = parseFloat(document.getElementById("f2").value);
    result = f1 / f2;
    showResult();
}
function showResult() {
    alert("Result = " + result);
}
</script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.