Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,51 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<!-- <script src="main.js"></script> -->
<title>Document</title>
<script src="main.js"></script>
<title>Calculator</title>
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
</style>
</head>
<body>
<form class="calculator">
<label for="first-number">First Number:</label>
<!-- the "onkeyup" event listener passes its input's "value" to the "saveFirstNumber" function in the main.js file -->
<input type="number" id="first-Number" name="first-Number" placeholder="type the first number" value="" onkeyup="saveFirstNumber(this.value)">
<label for="second-number">Second Number:</label>
<input type="number" id="second-Number" name="second-Number" placeholder="type the second number" onkeyup="saveSecondNumber(this.value)">
<div>

<div class="title">
<h1>Calculator</h1>
</div>

<div class="firstNum">
<label for="first-number">First Number:</label>
<!-- the "onkeyup" event listener passes its input's "value" to the "saveFirstNumber" function in the main.js file -->
<input type="number" id="first-Number" name="first-Number" placeholder="enter first number" value="" onkeyup="saveFirstNumber(this.value)">
</div>

<div class="secondNum">
<label for="second-number">Second Number:</label>
<input type="number" id="second-Number" name="second-Number" placeholder="enter second number" onkeyup="saveSecondNumber(this.value)">
</div>

<div class="buttons">
<!-- the "onclick" event listener passes its element's "id" to the "changeOperation" function in the main.js file -->
<button type="button" name="add" id="addition" onclick="changeOperation(this.id)">Add</button>
<button type="button" name="subtract" id="subtraction" onclick="changeOperation(this.id)">Subtract</button>
<button type="button" name="multiply" id="multiplication">Multiply</button>
<button type="button" name="divide" id="division">Divide</button>
<button type="button" name="modulus" id="modulus">Modulus</button>
<button type="button" name="multiply" id="multiplication" onclick="changeOperation(this.id)">Multiply</button>
<button type="button" name="divide" id="division" onclick="changeOperation(this.id)">Divide</button>
<button type="button" name="modulus" id="modulus" onclick="changeOperation(this.id)">Modulus</button>
</div>
<br>
<!-- this "onclick" calls the "equal" function in the main.js file -->
<button type="button" onclick="equals()">Equals</button>
<button type="reset">Clear</button>
<div class="equal-button">
<button type="button" class="equal" onclick="equals()">Equals</button>
</div>

<div class="reset-button">
<button type="reset" class="start-over" onclick="clearResults()">Clear</button>
</div>

</form>
<div id="result"></div>
</body>
Expand Down
35 changes: 29 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ const subtract = (numA, numB) => {
const multiply = (numA, numB) => {
// * to get a product then return it
// Open up the inspector tool in Chrome and select the Console tab to see what this functions is "logging out" to the console.
console.log(numA, numB)
// console.log(numA, numB)
const product = numA * numB
return product
}

const divide = null
const divide = (numA, numB) => {
// / to get a quotient,
const quotient = numA / numB
return quotient
}

const modulus = null
const modulus = (numA, numB) => {
// and % to get a remainder.
const remainder = numA % numB
return remainder
}


// This function changes the "operation" variable to be equal to the "id" of the button we choose on the web page.
const changeOperation = (chosenOperation) => {
Expand All @@ -57,18 +66,32 @@ const putResultInElement = (operationResults) => {
// Here we give it a string: "Results: " and add the value of the operation to it.
}

const clearResults = () => {
// access the DOM by writing "document" then use the method "getElementById" and pass it the id, "result".
document.getElementById("result").innerHTML = ""

// Remember, each element has built in properties like "innerHTML" which we can change to anything we like.
// Here we give it a string: "Results: " and add the value of the operation to it.
}

// The function uses the value of "operation" variable to determine which operation function it should use on the number: add, subtract, multiply, divide, or modulus
const equals = () => {
if (!firstNum) {
firstNum = parseInt(document.getElementById('first-Number').value)
}
if (!secondNum) {
secondNum = parseInt(document.getElementById('second-Number').value)
}
switch (operation) {
case "addition": putResultInElement(add(firstNum, secondNum))
break;
case "subtraction": putResultInElement(subtract(firstNum, secondNum))
break;
case "multiplication": multiply(firstNum, secondNum)
case "multiplication": putResultInElement(multiply(firstNum, secondNum))
break;
case "division": console.log(divide(firstNum, secondNum))
case "division": putResultInElement(divide(firstNum, secondNum))
break;
case "modulus": console.log(modulus(firstNum, secondNum))
case "modulus": putResultInElement(modulus(firstNum, secondNum))
break;
default: "Choose an operation"
}
Expand Down
85 changes: 85 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,89 @@ body {
margin: 20% auto;
width: 50%;
height: 75%;
border: 6px solid black;
background-color: goldenrod;
font-size: 35px;
-webkit-box-shadow: 10px 10px 0px 0px rgba(0,0,0,1);
-moz-box-shadow: 10px 10px 0px 0px rgba(0,0,0,1);
box-shadow: 10px 10px 0px 0px rgba(0,0,0,1);
}

.calculator {
display: grid;
grid-template-columns: 1fr;
grid-template-areas:
"title"
"num1"
"num2"
"butt"
"equalz"
"resetz"
}

.title {
grid-area: "title";
display: flex;
align-items: center;
justify-content: center;
}

.firstNum {
font-size: 18px;
grid-area: "num1";
display: flex;
align-items: center;
justify-content: center;
padding: 14px;
}

.secondNum {
font-size: 18px;
grid-area: "num2";
display: flex;
align-items: center;
justify-content: center;
padding: 14px;
}

.buttons {
grid-area: "butt";
display: flex;
align-items: center;
justify-content: center;
padding: 14px;
}

.equal-button {
grid-area: "equalz";
display: flex;
align-items: center;
justify-content: center;
padding: 14px;
}

.reset-button {
grid-area: "resetz";
display: flex;
align-items: center;
justify-content: center;
padding: 14px;
}

.equal{
height: 40px;
width:285px;
}

.start-over{
height: 40px;
width:285px;
}

.result {
font-size: 30px;
grid-area: "answer";
display: flex;
align-items: center;
justify-content: center;
}