Skip to content
Open

PR #77

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
117 changes: 87 additions & 30 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<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>
</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>
<!-- 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>
<br>
<!-- this "onclick" calls the "equal" function in the main.js file -->
<button type="button" onclick="equals()">Equals</button>
<button type="reset">Clear</button>
</form>
<div id="result"></div>
</body>
</html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />

<title>Document</title>
</head>
<body>
<form class="calculator">
<h4 class="greeting">Calculator</h4>
<div class="one">
<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="two">
<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="three">
<!-- 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>
</div>
<br />
<div class="four">
<!-- this "onclick" calls the "equal" function in the main.js file -->
<button type="button" onclick="equals()">Equals</button>
<!-- <button onclick="clear()">Clear</button> -->
<button type="button" id="plz">Clear</button>
</div>
<div class="five" id="result">Result:</div>
</form>
<script src="main.js"></script>
</body>
</html>
99 changes: 64 additions & 35 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,105 @@
// These variable hold the numbers we want to do operations on and the name of the operation we want to perform.
// They are expected to change so we use the "let" keyword.
let firstNum = null
let secondNum = null
let operation = null
let firstNum = null;
let secondNum = null;
let operation = null;

// this function takes in the number you type in the input field and saves it to the "firstNum" variable
const saveFirstNumber = (num) => {
firstNum = parseInt(num)
}
firstNum = parseInt(num);
};

// this function takes in the number you type in the 2nd input field and saves it to the "secondNum" variable
const saveSecondNumber = (num) => {
// "parseInt" is a built in function in JS that converts a string/word into a number
secondNum = parseInt(num)
}
secondNum = parseInt(num);
};

// this function takes in two argument/numbers and returns the sum of them
const add = (numA, numB) => {
const sum = numA + numB
return sum
}
const sum = numA + numB;
return sum;
};

// this function takes in two argument/numbers and returns the difference of them
const subtract = (numA, numB) => {
const difference = numA - numB
return difference
}
const difference = numA - numB;
return difference;
};

// 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) => {
// * 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 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);
const dividend = numA / numB;
return dividend;
};
// / to get a quotient,

const modulus = null
const modulus = (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);
const modulus = numA % numB;
return modulus;
};
// 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.
const changeOperation = (chosenOperation) => {
operation = chosenOperation
operation = chosenOperation;
// Use your Chrome Inspector Tool > Console Tab to see the "operation" that's logged
console.log(operation)
}
console.log(operation);
};

// 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 = "Results: " + operationResults;

// Remember, each element has built in properties like "innerHTML" which we can change to anything we like.
// 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 = () => {
switch (operation) {
case "addition": putResultInElement(add(firstNum, secondNum))
break;
case "subtraction": putResultInElement(subtract(firstNum, secondNum))
break;
case "multiplication": multiply(firstNum, secondNum)
break;
case "division": console.log(divide(firstNum, secondNum))
break;
case "modulus": console.log(modulus(firstNum, secondNum))
break;
default: "Choose an operation"
case "addition":
putResultInElement(add(firstNum, secondNum));
break;
case "subtraction":
putResultInElement(subtract(firstNum, secondNum));
break;
case "multiplication":
putResultInElement(multiply(firstNum, secondNum));
break;
case "division":
putResultInElement(divide(firstNum, secondNum));
break;
case "modulus":
putResultInElement(modulus(firstNum, secondNum));
break;
default:
"Choose an operation";
}
}
};


document.getElementById("plz").addEventListener("click", function (e) {
e.preventDefault();
document.getElementById("result").innerHTML = "Result:";
document.getElementById("first-Number").value = null;
document.getElementById("second-Number").value = null;
console.log(`heck yeah, no page reload.`);
});

45 changes: 43 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
body {
margin: 20% auto;
width: 50%;
height: 75%;
}
margin: 0 auto;
font-family: Arial, Helvetica, sans-serif;
}

form {
margin: auto auto;
grid-gap: 1px;
border: 1px solid black;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 0.25fr 1fr 1fr 1fr;
grid-template-areas:
"greeting greeting"
"one two"
"three four"
"five five";
grid-row-gap: 50px;
text-align: center;
}

.greeting {
grid-area: greeting;
}

.one {
grid-area: one;
}

.two {
grid-area: two;
}

.three {
grid-area: three;
}

.four {
grid-area: four;
}

.five {
grid-area: five;
}