1

I need help to create a function that:

  1. Contains information of the total sum of all numbers the user has input so far is calculated, printed to console, and stored on HTML page.
  2. When the total sum is larger than 100, the function prints "it's over" to console.

Any suggestions? Below is what I've got so far:

function adder() {
  var num1 = parseInt(document.getElementById("myform").elements["num1"].value);
  var num2 = parseInt(document.getElementById("myform").elements["num2"].value);
  var total = num1 + num2;
  document.getElementById("p1").innerHTML = total;
}
<form id="myform">
  First Number: <input type="text" name="num1"><br /><br /> Second Number: <input type="text" name="num2"><br /><br />
</form>
<button onclick="adder()"> Submit</button><br><br>
<p id="p1">Results Are Here</p>

1
  • please format your code Commented Sep 26, 2017 at 19:51

2 Answers 2

2

You should define and initialize total to 0 outside adder() so that you can add the fields values to the existing sum. Here's an example:

var total = 0;

function adder() {
  var num1 = parseInt(document.getElementById("myform").elements["num1"].value);
  var num2 = parseInt(document.getElementById("myform").elements["num2"].value);
  total += num1 + num2;
  document.getElementById("p1").innerHTML = total;
  if (total > 100) console.log("It's over!");
}
<form id="myform">
  First Number: <input type="text" name="num1"><br /><br /> Second Number: <input type="text" name="num2"><br /><br />
</form>
<button onclick="adder()"> Submit</button><br><br>
<p id="p1">Results Are Here</p>

On a side note if your <button> is part of your <form>, it will cause the page to refresh, so adding type="button" solves this issue in that case and you can see the result properly.

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

4 Comments

Why would his <button> submit the form, if it's not a submit button and it's outside the form?
thank you @Angelos Chalaris However, if user input 1 and 2, the line is updated to "3". Next time the user input 2 and 3, then the line is now "Current sum is 8" (3 + 2 + 3). This goes all the way until the sum is larger than 100. How to create that kind of function? Right now it just getting updated with new number each time user inputs it
@freginold You are absolutely right, my bad. I just updated my code, removing the fix, but keeping the comment at the bottom just in case it's part of the form. Thanks for pointing that out!
@AngelosChalaris - sorry I'm new here.. could you point me directions on how to mark it? :)
1

Quick mod to above answer to stop the counting after reaches > 100.

var total = 0;

function adder() {
  var num1 = parseInt(document.getElementById("myform").elements["num1"].value);
  var num2 = parseInt(document.getElementById("myform").elements["num2"].value);
  if (total > 100){
    console.log("It's over!");
  } else {
    total += num1 + num2;
    document.getElementById("p1").innerHTML = total;
  }
}
<form id="myform">
  First Number: <input type="text" name="num1"><br /><br /> Second Number: <input type="text" name="num2"><br /><br />
</form>
<button onclick="adder()"> Submit</button><br><br>
<p id="p1">Results Are Here</p>

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.