-
Notifications
You must be signed in to change notification settings - Fork 25
feat: calculator excercise tamar m. #21
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: main
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 |
|---|---|---|
| @@ -1,46 +1,44 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Calculator</title> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| </head> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Calculator</title> | ||
| <link rel="stylesheet" href="style.css"> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div class="calculator"> | ||
| <div class="display"> </div> | ||
| <div class="buttons"> | ||
| <div class="row"> | ||
| <button class="number" id="number-7">7</button> | ||
| <button class="number" id="number-8">8</button> | ||
| <button class="number" id="number-9">9</button> | ||
| </div> | ||
| <div class="row"> | ||
| <button class="number" id="number-4">4</button> | ||
| <button class="number" id="number-5">5</button> | ||
| <button class="number" id="number-6">6</button> | ||
| </div> | ||
| <div class="row"> | ||
| <button class="number" id="number-1">1</button> | ||
| <button class="number" id="number-2">2</button> | ||
| <button class="number" id="number-3">3</button> | ||
| </div> | ||
| <div class="row operations"> | ||
| <!-- <button id="add">+</button> | ||
| <button id="subtract">-</button> | ||
| <button id="multiply">*</button> | ||
| <button id="divide">/</button> --> | ||
| </div> | ||
| <div class="row last-row"> | ||
| <button id="clear">C</button> | ||
| <button class="number" id="number-0">0</button> | ||
| <button id="equal">=</button> | ||
| <body> | ||
| <div class="calculator"> | ||
| <div class="display"> </div> | ||
| <div class="buttons"> | ||
| <div class="row"> | ||
| <button class="number" id="number-7">7</button> | ||
| <button class="number" id="number-8">8</button> | ||
| <button class="number" id="number-9">9</button> | ||
| </div> | ||
| <div class="row"> | ||
| <button class="number" id="number-4">4</button> | ||
| <button class="number" id="number-5">5</button> | ||
| <button class="number" id="number-6">6</button> | ||
| </div> | ||
| <div class="row"> | ||
| <button class="number" id="number-1">1</button> | ||
| <button class="number" id="number-2">2</button> | ||
| <button class="number" id="number-3">3</button> | ||
| </div> | ||
| <div class="row operations"> | ||
| <button id="add">+</button> | ||
| <button id="subtract">-</button> | ||
| <button id="multiply">*</button> | ||
| <button id="divide">/</button> | ||
| </div> | ||
| <div class="row last-row"> | ||
| <button id="clear">C</button> | ||
| <button class="number" id="number-0">0</button> | ||
| <button id="equal">=</button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <script src="script.js"></script> | ||
| </body> | ||
|
|
||
| </html> | ||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* **************************************************** | ||
| * DevClub excercise 2 | ||
| * CALCULATOR | ||
| /* **************************************************** | ||
| * This file implements the logic of a caluclator | ||
| * that can do 4 basic operations: (+), (-), (*), (/) | ||
| /* **************************************************** */ | ||
|
|
||
| let operand; | ||
| let operator; | ||
| let result; | ||
|
|
||
| const display = document.querySelector('.display'); | ||
| const numbers = document.querySelectorAll('.number'); | ||
| const operators = document.querySelectorAll('.row.operations button'); | ||
|
|
||
| numbers.forEach((numButton) => { | ||
| numButton.addEventListener('click', () => { | ||
| if (display.textContent === '0') { | ||
|
Member
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.
|
||
| display.textContent = ''; | ||
| } | ||
|
|
||
| operand += numButton.textContent; | ||
|
|
||
| display.textContent += numButton.textContent; | ||
| }); | ||
| }); | ||
|
|
||
| operators.forEach((button) => { | ||
| button.addEventListener('click', () => { | ||
| calc(); | ||
|
|
||
| operator = button.textContent; | ||
|
Member
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. I would add a null check on the operator |
||
| display.textContent = result + operator; | ||
| }); | ||
| }); | ||
|
|
||
| clear.addEventListener('click', handleClear); | ||
|
|
||
| equal.addEventListener('click', () => { | ||
| calc(); | ||
| display.textContent = result; | ||
| operand = result; | ||
| result = 0; | ||
| }); | ||
|
|
||
| handleClear(); | ||
|
|
||
| function calc() { | ||
| switch (operator) { | ||
| case '+': | ||
| result = result + parseInt(operand); | ||
| break; | ||
| case '-': | ||
| result = result - parseInt(operand); | ||
| break; | ||
| case '*': | ||
| result = result * parseInt(operand); | ||
| break; | ||
| case '/': | ||
| result = result / parseInt(operand); | ||
|
Member
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. What will you see in case of |
||
| break; | ||
| default: | ||
| result = parseInt(operand); | ||
| } | ||
|
|
||
| operand = ''; | ||
| operator = ''; | ||
| } | ||
|
|
||
| function handleClear() { | ||
| operator = ''; | ||
| operand = ''; | ||
| result = 0; | ||
|
|
||
| display.textContent = '0'; | ||
|
Member
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. If you would removed the above |
||
| } | ||
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 is a good practice to get all of the numbers at once