-
Notifications
You must be signed in to change notification settings - Fork 25
finished calculator project #13
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 |
|---|---|---|
|
|
@@ -13,29 +13,29 @@ | |
| <div class="display"> </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> | ||
| <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> | ||
|
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 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> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| const display = document.querySelector(".display"); | ||
| let reset = true; | ||
|
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. reset sounds like a function, maybe call this |
||
|
|
||
| const calculate = () => { | ||
| let result = 0; | ||
|
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. 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("-"); | ||
|
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 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]); | ||
|
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. This could ne |
||
| 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; | ||
| } | ||
|
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 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"; | ||
|
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. Save your hardcoded strings into constant variables |
||
| }); | ||
|
|
||
| document.getElementById("equal").addEventListener("click", calculate); | ||
|
|
||
| } | ||
|
|
||
| init (); | ||
|
|
||
|
|
||
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.
Why do you need this property ?