[Solved] Find Sum of Two Numbers Using JavaScript in 1st one is manual entry and 2nd one is depend


Since I cannot find any pattern on the given criteria for the value of b, I suggest addressing this problem using ranges (although it is tiring). The criteria, however, fail if a > 10,000.

In order to address this, I’ve added a simple “catch” of alerting the user of incorrect input, and not to continue with the addition of the numbers (since no value for b was generated).

Below is the code:

function add() {
  var a, b, c;
  var within_range = true;
  a = Number(document.getElementById("first").value);
 
  if (a <= 200)
   b = 5;
  else if (a > 200 && a <= 1000)
   b = 10;
  else if (a > 1000 && a <= 2500)
   b = 20;
  else if (a > 2500 && a <= 5000)
   b = 50;
  else if (a > 5000 && a <= 10000)
   b = 1000;
  else {
   alert("First number must be less than or equal to 10,000.");
   within_range = false;
   document.getElementById("first").value = 10000;
  }
  
  if (within_range){
    c = a + b;
    document.getElementById("second").value = b;
    document.getElementById("answer").value = c;
  }
}
<input id="first">
<input id="second">
<button onclick="add()">Add</button>
<input id="answer">

1

solved Find Sum of Two Numbers Using JavaScript in 1st one is manual entry and 2nd one is depend