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
28 changes: 14 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@
<div class="display">&nbsp;</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>
<button class="number" data-value="7" id="number-7">7</button>

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.

Why do you need this property ?

<button class="number" data-value="8" id="number-8">8</button>
<button class="number" data-value="9" 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>
<button class="number" data-value="4" id="number-4">4</button>
<button class="number" data-value="5" id="number-5">5</button>
<button class="number" data-value="6" 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>
<button class="number" data-value="1" id="number-1">1</button>
<button class="number" data-value="2" id="number-2">2</button>
<button class="number" data-value="3" 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 data-value="+" id="add">+</button>

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 question in here, why are you using it?

<button data-value="-" id="subtract">-</button>
<button data-value="*" id="multiply">*</button>
<button data-value="/" id="divide">/</button>
</div>
<div class="row last-row">
<button id="clear">C</button>
<button class="number" id="number-0">0</button>
<button class="number" data-value="0" id="number-0">0</button>
<button id="equal">=</button>
</div>
</div>
Expand Down
66 changes: 66 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const display = document.querySelector(".display");
let reset = true;

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.

reset sounds like a function, maybe call this isReseted or something that will indicate the current state


const calculate = () => {
let result = 0;

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.

Result could be global variable (global for this file) so you will not create it every time again and again

fulleq = display.textContent;
if (fulleq.includes("*")){
let minieq = fulleq.split("*");
result = minieq[0];
for (let i=1; i< minieq.length; i++){
result *= minieq[i];
}
display.textContent = result;
}
if (fulleq.includes("/")){
let minieq = fulleq.split("/");
result = minieq[0];
for (let i=1; i< minieq.length; i++){
result /= minieq[i];
}
display.textContent = result;
}
if (fulleq.includes("+")){
let minieq = fulleq.split("+");
result = Number(minieq[0]);
for (let i=1; i< minieq.length; i++){
result += Number(minieq[i]);
}
display.textContent = result;
}
if (fulleq.includes("-")){
let minieq = fulleq.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.

You have 4 pieces of the same code, maybe extract it to function? the only different thing between them is the operator

result = Number(minieq[0]);

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.

This could ne NAN, better add a check for it

for (let i=1; i< minieq.length; i++){
result -= Number(minieq[i]);
}
display.textContent = result;
}


}

const init = () => {
document.querySelectorAll("[data-value]").forEach(btn => {
btn.addEventListener("click", () => {
if (reset === true) {
display.textContent = btn.dataset.value;
reset = false;
}

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 return in here you will not need the else

else
display.textContent += btn.dataset.value;
});
});

document.getElementById("clear").addEventListener("click", () => {
reset = true;
display.textContent = "0";

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 hardcoded strings into constant variables

});

document.getElementById("equal").addEventListener("click", calculate);

}

init ();