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
4 changes: 2 additions & 2 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="add">+</button>
<button id="subtract">-</button>
<button id="multiply">*</button>
<button id="divide">/</button> -->
<button id="divide">/</button>
</div>
<div class="row last-row">
<button id="clear">C</button>
Expand Down
74 changes: 74 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const numbers = document.querySelectorAll(".number");

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.

Good practice here to get all of the elements into 1 variable

const display = document.querySelector(".display");
numbers.forEach((number) => {
number.addEventListener("click", () => {
display.textContent += number.textContent;
});
});

const addButton = document.getElementById("add");

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 place all of your variables on top of your file

const subtractButton = document.getElementById("subtract");
const multiplyButton = document.getElementById("multiply");
const divideButton = document.getElementById("divide");
const clearButton = document.getElementById("clear");
const equalsButton = document.getElementById("equal");

addButton.addEventListener("click", () => {
display.textContent += "+";
});
subtractButton.addEventListener("click", () => {

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.

Can you think of a way to add event listener too all of your operators? just like you did above

display.textContent += "-";
});
multiplyButton.addEventListener("click", () => {
display.textContent += "*";

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.

Save your strings into constant variables

});
divideButton.addEventListener("click", () => {
display.textContent += "/";
});
clearButton.addEventListener("click", () => {
display.textContent = "";
});
equalsButton.addEventListener("click", () => {

const expression = display.textContent;
const parts = expression.split(/([+\-*/])/);

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.

Save the regex inside a variable


parts.forEach((part, i) => {

if(part == "*") {
const prevNumber = parseFloat(parts[i - 1]);
const nextNumber = parseFloat(parts[i + 1]);

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 check that this it not NAN

const result = prevNumber * nextNumber;
parts.splice(i - 1, 3, result);

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.

Fells like very specific logic, will it break if i add more parts? or add some complication to the assignment?

i -= 2;

} else if(part == "/") {
const prevNumber = parseFloat(parts[i - 1]);
const nextNumber = parseFloat(parts[i + 1]);
if (nextNumber === 0) {
display.textContent = "Error";
return;
}
const result = prevNumber / nextNumber;
parts.splice(i - 1, 3, result);
i -= 2;
}
});

parts.forEach((part, i) => {
if (part == "+") {
const prevNumber = parseFloat(parts[i - 1]);
const nextNumber = parseFloat(parts[i + 1]);

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 can extract those 2 lines into a function to avoid repeating yourself

const result = prevNumber + nextNumber;
parts.splice(i - 1, 3, result);
i -= 2;
} else if (part == "-") {
const prevNumber = parseFloat(parts[i - 1]);
const nextNumber = parseFloat(parts[i + 1]);
const result = prevNumber - nextNumber;
parts.splice(i - 1, 3, result);
i -= 2;
}
});
display.textContent = parts[0];
});