Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML homework help

Aleksxx03

New Coder
so i just started a IT class. We got given this assigment today and i dont know what to do.

Create a code where you can enter two numbers in two text boxes, and then get the sum (+), the difference (-), the product (*) and the quotient (/) when you press one of the corresponding four buttons.
 
- Need to create input boxes for whatever your 2 number will go.
- Need to create a button for each one of your math actions
- Need to create an output box next to your button to read the result
- Need to create a function for each math buttons, and output the results on the output box next to your button.

I created a similar codepen for formulas. You can modify it to your needs.


Formula CodePen
-
 
so i just started a IT class. We got given this assigment today and i dont know what to do.
Very strange. Classes normally list the requirements and don't let students sign up for a class they don't meet the requirements.

Like Elindo I used <input> to obtain the numbers. <input> is one of the things HTML needs to fix. Even using type+"number" does not limit the input to just numbers. You can put anything in the box, so the first thing we need to do is to make sure what is entered is numerical. This is the first part of the JavaScript. I set A and B to empty and make them global so I don't have to keep passing them all over the place. I use Listeners so the JS is all between the <script> tags and none in the HTML code. If things are good the check button is removed and the four math buttons are added.

The mid section runs the math as requested. Last section is another check on the user. If he/she changes the contents of the A - B <input>s the button revert back to their original state as determined by the CSS.

Any questions and I'd be happy to provide answers.

Code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Site</title>
<style>
p{display:inline;}
input[type=button]{display: none;}
#chck{display: inline;}
</style>
</head>
<body>
<label for="num1">Enter First Number</label>
<input type="text" id="num1"><p id="one"></p><br>

<label for="num2">Enter Second Number</label>
<input type="text" id="num2"><p id="two"></p>
<br><br>

<input type="button" id="chck" value="Validate the Numbers">

<input type="button" id="add"  class="math" value="Add Both Numbers = "><p id="addAns"></p><br>
<input type="button" id="sub"  class="math" value="Subtract Smaller from Larger = "><p id="subAns"></p><br>
<input type="button" id="mult" class="math" value="Multiply Both Numbers = "><p id="multAns"></p><br>
<input type="button" id="div"  class="math" value="Divide First by Second = "><p id="divAns"></p>


<script>
var A = '';
var B = '';
document.getElementById("chck").addEventListener("click", function() {
  document.getElementById("one").innerHTML="";
  document.getElementById("two").innerHTML="";

  A = document.getElementById('num1').value;
  if(isNaN(A)) document.getElementById("one").innerHTML="  Only enter numbers";

  B = document.getElementById('num2').value;
  if(isNaN(B)) document.getElementById("two").innerHTML="  Only enter numbers";

  if(!(isNaN(A)) && !(isNaN(B))){
    document.getElementById("chck").style.display = 'none';
    for(i=0; i<4; i++){
    document.getElementsByClassName("math")[i].style.display = 'inline';
    }
  }
});


document.getElementById("add").addEventListener("click", function() {
  document.getElementById("addAns").innerHTML = "    " + (Number(A) + Number(B));
});

document.getElementById("sub").addEventListener("click", function() {
  if( Number(A) > Number(B)){
    document.getElementById("subAns").innerHTML = "    " + (Number(A) - Number(B));
  }else{
  document.getElementById("subAns").innerHTML = "    " + (Number(B) - Number(A));
}});

document.getElementById("mult").addEventListener("click", function() {
  document.getElementById("multAns").innerHTML = "    " + (Number(A) * Number(B));
});

document.getElementById("div").addEventListener("click", function() {
  document.getElementById("divAns").innerHTML = "    " + (Number(A) / Number(B));
});

document.getElementById("num1").addEventListener("focusout", Myfunction);
document.getElementById("num2").addEventListener("focusout", Myfunction);

function Myfunction(){
  document.getElementById("chck").style.display = 'inline';
    for(i=0; i<4; i++){
    document.getElementsByClassName("math")[i].style.display = 'none';
    document.getElementsByTagName("p")[i+2].innerHTML = '';
    }
 }

</script>
</body>
</html>
 
Back
Top Bottom