0

I have this JavaScript program that is suppose to do very simple math from the user input (2 numbers, one of four operators - +,-,/,*). Problem is that the results are erratic (maybe not the best word because there is a pattern, but ahead with terminology) and I have no idea why.

By erratic I mean that, for example, if you start by summing 5+5 it will yield 10, as expected, but if you do a second try with 5+6 you will get 12, if you try 5+7 the result will be 14 and so on...

Any ideas on why this happens? Here is the code:

<HEAD>

<h2>A simple JavaScript powered calculator</h2>

<INPUT value="Enter a number" id="input1"></INPUT>
<SELECT id = "operator">
  <option id="choose" >choose </option>
  <option id="sum" value="+">sum</option>
  <option id="subtraction" value="-">subtraction</option>
  <option id="division" value="/">division</option>
  <option id="multiplication" value="*">multiplication</option>  
</SELECT>
<INPUT  value="Enter a number" id="input2"></INPUT>
<INPUT value= "calculate" type="button" onclick=result()></INPUT>
<INPUT id="outputfield" type="text"></INPUT>

</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript">
    function sum(number, number) {return number + number};
    function subtraction(number, number) {return number - number};
    function division(number, number) {return number / number};
    function multiplication(number, number) {return number * number};

    function result(){if (document.getElementById("operator").value === document.getElementById("sum").value) {console.log("its a +!"), document.getElementById("outputfield").value = 
                          sum(parseInt(document.getElementById("input1").value), parseInt(document.getElementById("input2").value));}

                     else if (document.getElementById("operator").value === document.getElementById("subtraction").value) {console.log("its a -!"), document.getElementById("outputfield").value = 
                          subtraction(parseInt(document.getElementById("input1").value), parseInt(document.getElementById("input2").value));}

                     else if (document.getElementById("operator").value === document.getElementById("division").value) {console.log("its a /!"), document.getElementById("outputfield").value = 
                          division(parseInt(document.getElementById("input1").value), parseInt(document.getElementById("input2").value));}

                     else if (document.getElementById("operator").value ===  document.getElementById("multiplication").value) {console.log("its a *!"), document.getElementById("outputfield").value = 
                          multiplication(parseInt(document.getElementById("input1").value), parseInt(document.getElementById("input2").value));}

                     else if (document.getElementById("operator").value === document.getElementById("choose").value) {console.log("please choose an operator");} 

                     } 

</SCRIPT>

<p> </>
<p> April 2016 </p>
</BODY>

Thanks in advance for any hints

3
  • Yeah. What do you think the functions will return? It does not make any sense. If a is 5 and a is 2, then what is a + a? 10? 4? Apr 16, 2016 at 0:08
  • This is honestly the funniest thing I've seen all day. Apr 16, 2016 at 0:10
  • @SverriM.Olsen, time to go to bed here :) thanks for bothering to point the obvious though :)
    – Lisdengard
    Apr 16, 2016 at 0:27

1 Answer 1

2

Your functions, for example:

function sum(number, number) {return number + number};

Use the same name in both parameters i.e. number, when you try to add 5 and 6, the name number is assigned the value 6 and since 6 + 6 = 12 the result is correct.

Try this instead:

function sum(number1, number2) {return number1 + number2};
1
  • 2
    And another thanks for sparing me the sarcasm ;) apparently some people here assume that everyone is an experienced programmer!
    – Lisdengard
    Apr 16, 2016 at 0:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.