Created
June 23, 2022 13:47
-
-
Save Pythonian/2ba074de4221270098e1f38c92c52764 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Javascript calculator</title> | |
</head> | |
<body> | |
<script> | |
// holds the kind of arithmetic operation to be performed | |
const operator = prompt("Which arithmetic operation do you wish to perform (+, -, * or /)? "); | |
// prompt the numbers for the computation from user | |
const number1 = parseInt(prompt("First number: ")); | |
const number2 = parseInt(prompt("Second number: ")); | |
// variable to hold the result of the computation | |
let result; | |
if (operator == "+") { | |
// addition operator | |
result = number1 + number2; | |
} else if (operator == "-") { | |
// subtraction operator | |
result = number1 - number2; | |
} else if (operator == "*") { | |
// multiplication operator | |
result = number1 * number2; | |
} else if (operator == "/") { | |
// division operator | |
result = number1 / number2; | |
} | |
else { | |
// if the operator entered is not among the options | |
alert("Invalid operator"); | |
} | |
// display the result to HTML | |
document.write("Computation: " + number1 + "" + operator + "" + number2 + " = " + result); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment