Skip to content

Instantly share code, notes, and snippets.

@thehandsomezebra
Created August 21, 2019 00:09
Show Gist options
  • Save thehandsomezebra/1d78d72fc14086451065808dace428ff to your computer and use it in GitHub Desktop.
Save thehandsomezebra/1d78d72fc14086451065808dace428ff to your computer and use it in GitHub Desktop.
Javascript_get-two-numbers-and-do-some-basic-math
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Practice JavaScript Math</title>
<link href="css/styles.css" rel="stylesheet">
</head>
<body>
<script src="js/math.js"></script>
</body>
</html>
// declare program variables
// announce the program
alert("Let's do some math!");
// collect numeric input
var num1 = prompt("Please type a number");
num1 = parseFloat(num1);
var num2 = prompt("Please type another number");
num2 = parseFloat(num2);
if (isNaN(num2) || isNaN(num2)) {
alert("one of both of your entries werent numbers. we will refresh and you should try again");
document.location.reload()
}
if (num2===0) {
num2 = prompt("it cant be zero. enter somethin else");
console.log(num2)
num2 = parseFloat(num2);
if (num2===0) {
console.log("is it getting here?")
alert("for real dude, if you divide by zero, you explode. just start over.")
document.location.reload()
}
}
// build an HTML message
var message = "<h1>Math with the numbers " + num1 + " and " + num2 + "</h1>";
message += num1 + " + " + num2 + " = " + (num1 + num2);
message += "<br>";
message += num1 + " * " + num2 + " = " + (num1 * num2);
message += "<br>";
message += num1 + " / " + num2 + " = " + (num1 / num2);
message += "<br>";
message += num1 + " - " + num2 + " = " + (num1 - num2);
// write message to web page
document.write(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment