JSFiddle - React, Tailwind, and code Playground
by Jeff Walsh
HTML
<h1>Learn Javascript Properly</h1>
<h3>Week One Assignment</h3>
<h5>Add, Multiply, Divide, or Subtract Two Numbers</h5>
Enter a number: <input type="number" id ="number1"> <br />
Enter a second number: <input type="number" id="number2">
<button id="mainButton">Let's Math!</button>
CSS
#mainButton {
margin-top: 1em;
}
#number2 {
margin-top: 0.5em;
}
JavaScript
//A function that does one of four basic math equations with user-entered numbers into an input box.
//Click function
document.getElementById('mainButton').onclick = function () {
//Getting values of inputs and saving them to variables
var numberOne = document.getElementByID('number1').value;
var numberTwo = document.getElementByID('number2').value;
//Setting values of the equations
var addition = (numberOne + numberTwo);
var subtraction = (numberOne - numberTwo);
var multiplication = (numberOne * numberTwo);
var division = (numberOne / numberTwo);
//Prompting user for their desired equation
var question = prompt("Do you wish to ADD, SUBTRACT, MULTIPLY, or DIVIDE?").toUpperCase();
//If statement to show the proper equation based on the user's prior prompt
if (question == ADD) {
alert('I added the shit out of those numbers for you - turns out it is ' + addition);
} else if (question == SUBTRACT) {
alert('Did some of this, some of that, some minusing - your answer is ' + subtraction);
} else if (question == MULTIPLY) {
alert('Yeah, I multipled the numbers, big whoop, wanna fight abouddit? the answers over there --> ' + multiplication);
} else if (question == DIVIDE) {
alert('This ones my favorite, I love a good division - ' + division);
};
};