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
45 changes: 28 additions & 17 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,41 @@
<head>
<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>

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Rubik+80s+Fade&display=swap" rel="stylesheet">

<script src="main.js"></script>

<title>Calculator</title>
</head>
<body>
<h1 class="title">CALCULATOR</h1>

<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>
<!-- 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>
<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="type the 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="type the second number" onkeyup="saveSecondNumber(this.value)">
</div>
<!-- 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" 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>
<br>
<!-- this "onclick" calls the "equal" function in the main.js file -->
<button type="button" onclick="equals()">Equals</button>
<button type="reset">Clear</button>
<button type="button" onclick="equals()" id="equalsign">Equals</button>
<button type="reset" id="clear" onclick="clearResult()" >Clear</button>
<div id="result"></div>
</form>
<div id="result"></div>
</body>
</html>
30 changes: 20 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ const subtract = (numA, numB) => {
// These variables are already defined but that don't point to functions. It's up to you to build the functions to complete your calculator use:

const multiply = (numA, numB) => {
console.log(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)
return numA * numB;
}

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

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

// This function changes the "operation" variable to be equal to the "id" of the button we choose on the web page.
Expand All @@ -51,7 +56,7 @@ const changeOperation = (chosenOperation) => {
// In order to show the user their results we have to access the DOM and stick in the value
const putResultInElement = (operationResults) => {
// access the DOM by writing "document" then use the method "getElementById" and pass it the id, "result".
document.getElementById("result").innerHTML = "Results: " + operationResults
document.getElementById("result").innerHTML = "Result: " + operationResults;

// 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.
Expand All @@ -60,17 +65,22 @@ const putResultInElement = (operationResults) => {
// 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 = () => {
switch (operation) {
case "addition": putResultInElement(add(firstNum, secondNum))
case "addition": putResultInElement(add(firstNum, secondNum));
break;
case "subtraction": putResultInElement(subtract(firstNum, secondNum))
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"
default: "Choose an operation";
}
}

// A function that will handle the clear operation.
const clearResult = () => {
console.log('test')
document.getElementById("result").innerHTML = "";
}
91 changes: 90 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,94 @@
body {
margin: 20% auto;
margin: auto;
padding-top: 50px;
width: 50%;
height: 75%;
background-color: #FFF;
color: #207487;
}

.title {
margin: auto;
width: 100%;
text-align: center;
padding-bottom: 40px;
}

.calculator {
background-color: #207487;
border-radius: 10px;
padding: 50px;
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 50px 1fr 1fr 1fr;
grid-row-gap: 20px;
grid-template-areas:
"result result result result"
"num1 num1 num2 num2"
"addition subtraction division multiplication"
"modulo modulo equals equals"
"clear clear clear clear";
}

.calculator, .calculator button {
font-family: 'Hubballi', sans-serif;
font-size: 20px;
color: white;
}

.calculator button {
color: #BF5151;
}

.firstnum {
text-align: center;
grid-area: num1;
}

.secondnum {
text-align: center;
grid-area: num2;
}

#addition {
grid-area: addition;
}

#subtraction {
grid-area: subtraction;
}

#division {
grid-area: division;
}

#multiplication {
grid-area: multiplication;
}

#modulus {
grid-area: modulo;
}

#equalsign {
grid-area: equals;
}

#clear {
grid-area: clear;
}

#result {
grid-area: result;
border-radius: 10px;
background-color: white;
height: 40px;
color: rgb(76, 71, 71);
text-align: center;
font-size: 30px;
}

.title {
font-family: 'Hubballi', sans-serif;
font-size: 60px;
}