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
37 changes: 22 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@
<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> -->
<script src="main.js"></script>
<title>Document</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="result"></div>
<form class="calculator">
<label for="first-number">First Number:</label>
<label for="first-number">Calculator</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>
<input type="number" id="first-Number" name="first-Number" placeholder="type here" value="" onkeyup="saveFirstNumber(this.value)">


<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="add" id="addition" onclick="changeOperation(this.id)">+</button>
<button type="button" name="subtract" id="subtraction" onclick="changeOperation(this.id)">-</button>
<button type="button" name="multiply" id="multiplication" onclick="changeOperation(this.id)">x</button>
<button type="button" name="divide" id="division" onclick="changeOperation(this.id)">/</button>
<button type="button" name="modulus" id="modulus" onclick="changeOperation(this.id)">%</button>
</div>


<label for="second-number"></label>
<input type="number" id="second-Number" name="second-Number" placeholder="type here" onkeyup="saveSecondNumber(this.value)">
<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="equalOrClear">
<button type="button" onclick="equals()">=</button>
<button type="reset" onclick="clearResult()">Clear</button>
</div>
</form>
<div id="result"></div>
</body>
</html>
27 changes: 20 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ const subtract = (numA, numB) => {

const multiply = (numA, numB) => {
// * to get a product then return it
const product = numA * numB
// 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 product
}

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

const modulus = null
const modulus = (numA, numB) => {
const remainder = numA % numB
return remainder
}
// 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 +58,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 = 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 @@ -64,13 +71,19 @@ const equals = () => {
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"
}
}

function clearResult() {
firstNum = null
secondNum = null
operation = null
result.innerHTML = null
}
35 changes: 35 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,39 @@ body {
margin: 20% auto;
width: 50%;
height: 75%;
display: flex;
flex-direction: column;
}

form.calculator {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
background-color: grey;
border-radius: 5px;
}
div.buttons {
display: flex;
justify-content: space-evenly;
align-items: center;
margin: 2%;
/* flex-direction: column; */
}

div.equalOrClear {
display: flex;
justify-content: space-around;
align-items: center;
margin: 2%;
/* flex-direction: column; */
}

#result {
display: flex;
justify-content: center;
align-items: center;
color: red;
background-color: rgb(214, 212, 212);
border-radius: 5px;
}