Skip to content

Add initial calculator functionality with operator buttons and event …#20

Open
reutmaduel114-collab wants to merge 1 commit into
ColmanDevClubORG:mainfrom
reutmaduel114-collab:homework/calculator-extension/reut
Open

Add initial calculator functionality with operator buttons and event …#20
reutmaduel114-collab wants to merge 1 commit into
ColmanDevClubORG:mainfrom
reutmaduel114-collab:homework/calculator-extension/reut

Conversation

@reutmaduel114-collab

Copy link
Copy Markdown

…handling

@Tamir198 Tamir198 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey left you some changes

Comment thread .vscode/settings.json
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501

Copy link
Copy Markdown
Member

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

Comment thread script.js
numbers.forEach(btn => {
btn.addEventListener('click', () => {
const value = btn.textContent;
console.log(currentOperator)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the console log

Comment thread script.js
const value = btn.textContent;
console.log(currentOperator)
if (currentOperator == null) {
FirstNumber = (FirstNumber ?? "") + value;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variables should not start with capital letter

Comment thread script.js
console.log(`FirstNumber ${FirstNumber}`);
display.textContent = FirstNumber;
}
else {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Comment thread script.js
});


plus.addEventListener('click', function () {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Comment thread script.js

if (['+', '-', '*', '/'].includes(lastChar)) {
display.textContent = text.slice(0, -1) + currentOperator; //replace last operator with new one
} else {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

Comment thread script.js
SecondNumber = null;
currentOperator = null;
result = null;
display.innerHTML = '&nbsp'; // Keep display from collapsing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never use innerHTML its very dangerous method

Comment thread script.js

equal.addEventListener('click', function () {
if (currentOperator == '+') {
result = Number(FirstNumber) + Number(SecondNumber);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add check for NAN

Comment thread script.js
result = Number(FirstNumber) + Number(SecondNumber);
}

if (currentOperator == '-') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use else if to avoid check all of the ifs

Comment thread script.js
if (currentOperator == '/') {
result = Number(FirstNumber) / Number(SecondNumber);
}
const limitedResult = Number(result.toFixed(8)); // Limit to 8 decimal places

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 should be a constant variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants