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
123 changes: 94 additions & 29 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,97 @@
<!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>
<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" defer></script>
<title>Calculator</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=Grape+Nuts&display=swap"
rel="stylesheet"
/>
</head>
<body>
<header>
<h1>Welcome to Stephen's Rinky Dink Calculator</h1>
</header>
<div id="page-container">
<form class="calculator">
<label for="first-number" class="first-number-label"
>First Number:</label
>
<!-- the "onkeyup" event listener passes its input's "value" to the "saveFirstNumber" function in the main.js file -->
<input
class="first-number-input"
type="number"
id="first-Number"
name="first-Number"
placeholder="type the first number"
value=""
onkeyup="saveFirstNumber(this.value)"
/>
<label for="second-number" class="second-number-label"
>Second Number:</label
>
<input
class="second-number-input"
type="number"
id="second-Number"
name="second-Number"
placeholder="type the second number"
onkeyup="saveSecondNumber(this.value)"
/>
<div id="operations">
<!-- 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)"
>
+
</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)"
>
&#247;
</button>
<button
type="button"
name="modulus"
id="modulus"
onclick="changeOperation(this.id)"
>
%
</button>
</div>
<!-- <br /> -->
<!-- this "onclick" calls the "equal" function in the main.js file -->
<button type="button" onclick="equals()" class="equals">Equals</button>
<button type="reset" class="clear">Clear</button>
</form>
<div id="result"></div>
</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>
</body>
</html>
102 changes: 63 additions & 39 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,100 @@
// 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 = parseFloat(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 = parseFloat(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 (Math.round(sum * 1000000) / 1000000).toLocaleString('en-US');
};

// 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 (Math.round(difference * 1000000) / 1000000).toLocaleString('en-US');
};

// 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)
}
const product = numA * numB;
return (Math.round(product * 1000000) / 1000000).toLocaleString('en-US');
};

const divide = null
// / to get a quotient,
const divide = (numA, numB) => {
const quotient = numA / numB;
return (Math.round(quotient * 1000000) / 1000000).toLocaleString('en-US');
};

const modulus = null
// and % to get a remainder.
const modulus = (numA, numB) => {
const modulus = numA % numB;
return (Math.round(modulus * 1000000) / 1000000).toLocaleString('en-US');
};

// 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
// Use your Chrome Inspector Tool > Console Tab to see the "operation" that's logged
console.log(operation)
}
operation = chosenOperation;
// Use your Chrome Inspector Tool > Console Tab to see the "operation" that's
// logged
// if (operation === addition) {
// operation = '+';
// } else if (operation === subtraction) {
// operation = '-';
// } else if (operation === multiplication) {
// operation = 'x';
// } else if (operation === division) {
// operation = '&#247';
// } else if (operation === modulus) {
// 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 = `The answer is: ${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';
}
}

};
Loading