could not get the addition result

Related searches

I want to add the user input but there is something wrong. there is no errors but nothing happens. I want to make sure that the input is numbers and then add the two inputs together.

function add() {
  
  var first = document.getElementById('first-number').value;        
  var second = document.getElementById('second-number').value;        

   
        firstMostBe = first;
        secondMostBe = second;
          var output;  
   if ((isNaN(firstMostBe)) || (isNaN(secondMostBe))){
     alert('one or both of the operands are not numbers');
   } else {
         output = firstMostBe + secondMostBe;
         return output;  
   } 
 
  var result = output; // place-holder, delete this and start over
  var output_field = document.getElementById('result');
  output_field.innerHTML = result;
}
<h2>Three-Layer Handlers Exercises</h2>

<hr>

  
   

<script src="./handlers-calculator.js"></script>

first number: <input id='first-number' value=''></input><br>
second number: <input id='second-number' value=''></input> <br>
<button onclick='add()'>+</button> 
<button onclick='subtract()'>-</button> 
<button onclick='multiply()'>*</button> 
<button onclick='divide()'>/</button> <br>
<p id='result'></p>

<br><br>

<hr>  
<hr>  
   

You need the numerical value (with an unary plus +) of the operands and you need to return early, if you have NaN values.

Later you need to assign the result of the operation to the target element.

Inbetween, you need not to return, because this prevents to run until the place where the result is assigned.

function add() {
    var first = +document.getElementById('first-number').value,   // try to get a number
        second = +document.getElementById('second-number').value, // try to get a number
        output_field = document.getElementById('result');

    if (isNaN(first) || isNaN(second)) {
        alert('one or both of the operands are not numbers');
        return;                                                   // exit
    }

    output_field.innerHTML = first + second;
}
<h2>Three-Layer Handlers Exercises</h2>
first number: <input id='first-number' value=''><br>
second number: <input id='second-number' value=''><br>
<button onclick='add()'>+</button> 
<button onclick='subtract()'>-</button> 
<button onclick='multiply()'>*</button> 
<button onclick='divide()'>/</button> <br>
<p id='result'></p>

How does Java handle integer underflows and overflows and how , When adding two int's, you will get no indication when an overflow occurs. (a + b) the addition would use int precision and // the result would be cast to long� Add To: Result Unknown Materials: Add To: Result Unknown (within 20) word problem cards _____ 1. Solve the word problems. For each card: a) represent the problem with an equation using a symbol to represent the unknown number b) use objects or drawings to model the problem and find

function add() {

    var first = document.getElementById('first-number').value;
    var second = document.getElementById('second-number').value;


    firstMostBe = first;
    secondMostBe = second;
    var output;
    if (isNaN(firstMostBe) || isNaN(secondMostBe)) {
        alert('one or both of the operands are not numbers');
    } else {
        output = parseInt(firstMostBe) + parseInt(secondMostBe);// By defaut it is string need to convert to number
        // no need to return form here 
    }

    var result = output; // place-holder, delete this and start over
    var output_field = document.getElementById('result');
    output_field.innerHTML = result;
}

2 issues are there

  1. You are returning in between of the function
  2. You are not casting the values to Number (by default it is string)

Errors in DNA Replication, Moreover, when the genes for the DNA repair enzymes themselves become That might not seem like much, until you consider how much DNA a cell has. during DNA replication, a DNA strand may loop out, resulting in the addition or� When two variables are multiplied, the result is called a product. When they are divided, it is a quotient. Addition results in a sum and subtraction results in a difference.

Excel formulas not working: how to fix formulas not updating or not , Don't worry, most likely your Excel is all right, and you will get all the Symptoms : Excel formula not working correctly, it returns an error or a wrong result. SUM formula may not work because of numbers formatted as text:. Add To: Result Unknown (within 10) Materials: Add To: Result Unknown (within 10) word problem cards _____ 1. Solve the word problems. For each card: a) read the problem with a partner b) use objects or drawings to model the problem c) write a number sentence to match your model 2.

Result and Error Codes, Given a full 32-bit extended result code, the application can always find the In addition to being a result code, the SQLITE_ABORT value is also used as a conflict The SQLITE_BUSY result code indicates that the database file could not be� The nasal swab tests took minutes to administer, but he did not get results for another 16 days, he said on social media. Getting test results also delayed Such long testing delays are not unusual

Unemployment filing failures: New survey confirms that millions of , Three to four additional people tried to apply but could not get through the The survey results therefore imply there were about 23.8 million UI� The URL of the page to be converted is not properly resolved on the server, even though it resolves properly on the client. The file size may be too large. If this is the cause, a change to web.config may resolve the problem. Possible solution for file size too large:

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.

Comments
  • thanks a lot. I actually figured out about the parseInt part. then with your note i realized i was terminating my func before it finishes it is work.
  • if it resolved your issue can you please mark it as accepted
  • could you please explain why the array declaration looks like this: var [first,second]
  • @ab2016d Its called Destructuring Assignment.
  • @ab2016d See I have updated the answer with little more detail about that part.
  • you are the best. this is really insightful. thank you.