-
Notifications
You must be signed in to change notification settings - Fork 250
Update calculator app #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,31 +2,38 @@ | |
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <!-- <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> | ||
| </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> | ||
| <main> | ||
| <div id="result">Look right here when you're done!</div> | ||
| <form class="calculator"> | ||
| <div class="number-container"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can be section tags instead of divs. |
||
| <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 --> | ||
| <!-- Note that "this" refers to the whole element, including the opening and closing tag --> | ||
| <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="operation-container"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Section could be great here! Also, do we use operation container on any other element in this document? It might be better to implement an ID selector. |
||
| <!-- 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> | ||
| <!-- this "onclick" calls the "equal" function in the main.js file --> | ||
| <div class="finish"> | ||
| <button type="button" onclick="equals()">Equals</button> | ||
| <button type="reset">Clear</button> | ||
| </div> | ||
| </form> | ||
| </main> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,14 +32,21 @@ 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) | ||
| const product = numA * numB | ||
| return product | ||
| } | ||
|
|
||
| const divide = null | ||
| const divide = (numA, numB) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent execution of logic to perform these operations! |
||
| // / 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) => { | ||
|
|
@@ -55,20 +62,29 @@ const putResultInElement = (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. | ||
|
|
||
|
|
||
| // NOTE TO SELF: | ||
| // You could already have Results as innerHTML that is always rendered on the page (already in the html file), and then append operationResults to it | ||
| // e.g. <div id="result">Results: </div> | ||
| // let text = document.createTextNode(operationResults) | ||
| // document.getElementById("result").appendChild(text) | ||
| } | ||
|
|
||
| // 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 | ||
| // We can use operation, firstNum, and secondNum after we saved them because these variables have global scope | ||
| const equals = () => { | ||
| switch (operation) { | ||
| case "addition": putResultInElement(add(firstNum, secondNum)) | ||
| break; | ||
| case "subtraction": putResultInElement(subtract(firstNum, secondNum)) | ||
| break; | ||
| case "multiplication": multiply(firstNum, secondNum) | ||
| // If wanted to log in console instead, do console.log(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" | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,59 @@ | ||
| body { | ||
| margin: 20% auto; | ||
| width: 50%; | ||
| height: 75%; | ||
|
|
||
| width: 300pt; | ||
| } | ||
|
|
||
| main { | ||
| background-color: lightblue; | ||
| } | ||
|
|
||
| #result { | ||
| height: 20pt; | ||
| background-color: rgb(243, 106, 129); | ||
| padding: 20pt; | ||
| } | ||
|
|
||
| form { | ||
| width: 100%; | ||
| } | ||
|
|
||
| .number-container { | ||
| border: rgb(20, 106, 219) 2pt solid; | ||
| display: grid; | ||
| grid-template-areas: 'one two' | ||
| 'three four'; | ||
| height: 60pt; | ||
| padding: 10pt; | ||
| background-color: rgb(184, 235, 231); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of grid here!! |
||
| .number-container * { | ||
| margin: 2pt 8pt; | ||
| align-content: center; | ||
| /* text-align: center; */ | ||
| } | ||
|
|
||
| .operation-container { | ||
| display: grid; | ||
| grid-template-areas: 'one two' | ||
| 'three four' | ||
| 'five . '; | ||
| margin: 10pt; | ||
| } | ||
|
|
||
| .operation-container button { | ||
| margin: 2pt; | ||
| height: 30pt; | ||
| } | ||
|
|
||
| .finish { | ||
| padding: 10pt; | ||
| } | ||
|
|
||
| .finish button { | ||
| margin: 2pt; | ||
| height: 20pt; | ||
| width: 70pt; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can also use a section tag.