-
Notifications
You must be signed in to change notification settings - Fork 25
updated calc logic #17
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| const numbers = document.querySelectorAll(".number"); | ||
| const display = document.querySelector(".display"); | ||
| numbers.forEach((number) => { | ||
| number.addEventListener("click", () => { | ||
| display.textContent += number.textContent; | ||
| }); | ||
| }); | ||
|
|
||
| const addButton = document.getElementById("add"); | ||
|
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 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", () => { | ||
|
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. 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 += "*"; | ||
|
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 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(/([+\-*/])/); | ||
|
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 the regex inside a variable |
||
|
|
||
| parts.forEach((part, i) => { | ||
|
|
||
| if(part == "*") { | ||
| const prevNumber = parseFloat(parts[i - 1]); | ||
| const nextNumber = parseFloat(parts[i + 1]); | ||
|
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 should check that this it not |
||
| const result = prevNumber * nextNumber; | ||
| parts.splice(i - 1, 3, result); | ||
|
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. 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]); | ||
|
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 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]; | ||
| }); | ||
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.
Good practice here to get all of the elements into 1 variable