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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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

}
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<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> -->
<button class = "operator" id="add">+</button>
<button class = "operator" id="subtract">-</button>
<button class = "operator" id="multiply">*</button>
<button class = "operator" id="divide">/</button>
</div>
<div class="row last-row">
<button id="clear">C</button>
Expand Down
105 changes: 105 additions & 0 deletions script.js
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)

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

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

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

SecondNumber = (SecondNumber ?? "") + value;
console.log(`SecondNumber ${SecondNumber}`);
display.textContent += value;
}
});
});


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?

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

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.

Is this Ai code? or ai comment?
Any way remove it - the code should explain itself no need for comments

const text = display.textContent;
const lastChar = text[text.length - 1]; //get last character to check if it's an operator

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.

Same here

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

display.textContent += currentOperator;
}

console.log(`operator ${currentOperator}`);
}



clear.addEventListener('click', function () {
FirstNumber = null;
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


console.clear()
console.log('clear');
});

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

}

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

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

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

display.textContent = limitedResult;
console.log(result)
FirstNumber = null;
SecondNumber = null;
currentOperator = null;
});