-
Notifications
You must be signed in to change notification settings - Fork 25
Add initial calculator functionality with operator buttons and event … #20
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "liveServer.settings.port": 5501 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| let FirstNumber, SecondNumber; | ||
| let result; | ||
| let currentOperator = null; | ||
|
|
||
| let display = document.querySelector('.display') | ||
| let numbers = document.querySelectorAll('.number') | ||
| let plus = document.querySelector('#add') | ||
| let equal = document.querySelector('#equal') | ||
| let divide = document.querySelector('#divide') | ||
| let subtract = document.querySelector('#subtract') | ||
| let multiply = document.querySelector('#multiply') | ||
| let clear = document.querySelector('#clear') | ||
| let operator = document.querySelectorAll('.operator') | ||
|
|
||
|
|
||
| numbers.forEach(btn => { | ||
| btn.addEventListener('click', () => { | ||
| const value = btn.textContent; | ||
| console.log(currentOperator) | ||
|
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. Remove the console log |
||
| if (currentOperator == null) { | ||
| FirstNumber = (FirstNumber ?? "") + value; | ||
|
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. variables should not start with capital letter |
||
| console.log(`FirstNumber ${FirstNumber}`); | ||
| display.textContent = FirstNumber; | ||
| } | ||
| else { | ||
|
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 will add a return above you will have no need for this else block |
||
| SecondNumber = (SecondNumber ?? "") + value; | ||
| console.log(`SecondNumber ${SecondNumber}`); | ||
| display.textContent += value; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| plus.addEventListener('click', function () { | ||
|
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. Instead of repeating the event listener addition, can you create a single function like you did on top? |
||
| currentOperator = '+'; | ||
| updateOperatorDisplay(); | ||
| }); | ||
|
|
||
| subtract.addEventListener('click', function () { | ||
| currentOperator = '-'; | ||
| updateOperatorDisplay(); | ||
| }); | ||
|
|
||
| multiply.addEventListener('click', function () { | ||
| currentOperator = '*'; | ||
| updateOperatorDisplay(); | ||
| }); | ||
|
|
||
| divide.addEventListener('click', function () { | ||
| currentOperator = '/'; | ||
| updateOperatorDisplay(); | ||
| }); | ||
|
|
||
|
|
||
| function updateOperatorDisplay() { | ||
| if (!FirstNumber && FirstNumber !== 0) return; //the second check is for 0 value, that pass the first condition | ||
|
|
||
|
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. Is this Ai code? or ai comment? |
||
| const text = display.textContent; | ||
| const lastChar = text[text.length - 1]; //get last character to check if it's an operator | ||
|
|
||
|
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. Same here |
||
| if (['+', '-', '*', '/'].includes(lastChar)) { | ||
| display.textContent = text.slice(0, -1) + currentOperator; //replace last operator with new one | ||
| } else { | ||
|
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. And here |
||
| display.textContent += currentOperator; | ||
| } | ||
|
|
||
| console.log(`operator ${currentOperator}`); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| clear.addEventListener('click', function () { | ||
| FirstNumber = null; | ||
| SecondNumber = null; | ||
| currentOperator = null; | ||
| result = null; | ||
| display.innerHTML = ' '; // Keep display from collapsing | ||
|
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. Never use |
||
|
|
||
| console.clear() | ||
| console.log('clear'); | ||
| }); | ||
|
|
||
| equal.addEventListener('click', function () { | ||
| if (currentOperator == '+') { | ||
| result = Number(FirstNumber) + Number(SecondNumber); | ||
|
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. You should add check for |
||
| } | ||
|
|
||
| if (currentOperator == '-') { | ||
|
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. Use |
||
| result = Number(FirstNumber) - Number(SecondNumber); | ||
| } | ||
|
|
||
| if (currentOperator == '*') { | ||
| result = Number(FirstNumber) * Number(SecondNumber); | ||
| } | ||
|
|
||
| if (currentOperator == '/') { | ||
| result = Number(FirstNumber) / Number(SecondNumber); | ||
| } | ||
| const limitedResult = Number(result.toFixed(8)); // Limit to 8 decimal places | ||
|
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. 8 should be a constant variable |
||
| display.textContent = limitedResult; | ||
| console.log(result) | ||
| FirstNumber = null; | ||
| SecondNumber = null; | ||
| currentOperator = null; | ||
| }); | ||
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.
You dont have to push this file