Skip to content

Commit 2b00a7e

Browse files
authored
Merge pull request #34 from josueJURE/toggle-negative-positive
Toggle negative positive
2 parents 02fcc50 + 765ce6a commit 2b00a7e

File tree

2 files changed

+68
-23
lines changed

2 files changed

+68
-23
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div data-screen class="screen" aria-label="Calculator Screen">0</div>
1212
<div class="sliding-part">
1313
<button data-value="AC" class="child symbol" aria-label="All Clear">AC</button>
14-
<button data-value="minus" class="child symbol" aria-label="Toggle Sign">+/-</button>
14+
<button data-value="minus" class="child symbol minus" aria-label="Toggle Sign">+/-</button>
1515
<button data-value="%" data-operator class="child symbol" aria-label="Percentage">%</button>
1616
<button data-value="/" data-number class="child operator" aria-label="Divide">÷</button>
1717
<button data-value="7" class="child digit" aria-label="Number 7">7</button>

index.js

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ const historyElement = document.querySelector(".computation-history");
33
let screen = document.querySelector("[data-screen]");
44
const historyBtn = document.querySelector(".history-btn");
55
const slidingPart = document.querySelector(".sliding-part");
6-
const computationHistoryParent = document.querySelector(".computation-history-parent");
6+
const computationHistoryParent = document.querySelector(
7+
".computation-history-parent"
8+
);
79
const operators = document.querySelectorAll("[data-operator]");
810
const clearHistoryBtn = document.querySelector(".clear-history-btn");
9-
let currentExpression
11+
const minus = document.querySelector(".minus");
12+
console.log(minus);
13+
14+
let currentExpression;
1015

1116
clearHistoryBtn.addEventListener("click", () => {
1217
historyElement.innerHTML = "";
@@ -84,9 +89,10 @@ btns.forEach((btn) => {
8489
// forEach ends & functions creations begins
8590
function convertToPercentage(button) {
8691
if (button === "%") {
87-
currentExpression = data.join('');
92+
currentExpression = data.join("");
8893
currentExpression = currentExpression / 100;
8994
data = [currentExpression];
95+
console.log(data);
9096
screen.innerText = currentExpression;
9197
}
9298
}
@@ -133,28 +139,67 @@ function deleteEverythingFromScreen(button) {
133139
}
134140
}
135141

142+
// function toggleSign(button) {
143+
// if (button === "minus") {
144+
// console.log(data[0])
145+
// currentExpression = data.join("");
146+
// console.log(currentExpression)
147+
// let reversedExpression = currentExpression.split("").join("");
148+
// console.log(reversedExpression)
149+
// let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit
150+
151+
// if (match) {
152+
// let start = currentExpression.length - match[ZERO].length;
153+
// let end = currentExpression.length;
154+
// let currentValue = Number(match[ZERO]);
155+
156+
// if (!isNaN(currentValue)) {
157+
// currentValue = -currentValue;
158+
// data = data
159+
// .slice(ZERO, start)
160+
// .concat(currentValue.toString().split(""), data.slice(end));
161+
// screen.innerText = data.join("");
162+
// }
163+
// }
164+
// }
165+
// }
166+
167+
// The unshift() method of Array instances adds the specified elements to the beginning
168+
// of an array and returns the new length of the array.
169+
170+
// The shift() method of Array instances removes the first element from an array and returns
171+
// that removed element. This method changes the length of the array.
172+
173+
minus.addEventListener("click", toggleSign);
174+
175+
// function toggleSign(button) {
176+
// let value = Number(data.join(""))
177+
// if (button === "minus") {
178+
// if (value > 0) {
179+
// value = -value
180+
// console.log(value);
181+
// screen.innerText = value;
182+
// } if (value < 0) {
183+
// console.log("smaller than zero")
184+
// value = value * -1
185+
// console.log(value)
186+
// screen.innerText = value;
187+
// }
188+
// // screen.innerText = value;
189+
// data = [value]
190+
// }
191+
// }
192+
136193
function toggleSign(button) {
194+
let value = Number(data.join(""));
137195
if (button === "minus") {
138-
console.log(data)
139-
currentExpression = data.join("");
140-
console.log(currentExpression)
141-
let reversedExpression = currentExpression.split("").join("");
142-
console.log(reversedExpression)
143-
let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit
144-
145-
if (match) {
146-
let start = currentExpression.length - match[ZERO].length;
147-
let end = currentExpression.length;
148-
let currentValue = Number(match[ZERO]);
149-
150-
if (!isNaN(currentValue)) {
151-
currentValue = -currentValue;
152-
data = data
153-
.slice(ZERO, start)
154-
.concat(currentValue.toString().split(""), data.slice(end));
155-
screen.innerText = data.join("");
156-
}
196+
if (value > 0) {
197+
value = -value;
198+
} else if (value < 0) {
199+
value = value * -1;
157200
}
201+
screen.innerText = value;
202+
data = [value];
158203
}
159204
}
160205

0 commit comments

Comments
 (0)