Skip to content

Commit

Permalink
added support for negative numbers. need to clean up code and rename …
Browse files Browse the repository at this point in the history
…variables.
  • Loading branch information
angaaruriakhil committed Sep 21, 2021
1 parent 84da27d commit 25fbf65
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 41 deletions.
79 changes: 56 additions & 23 deletions dist/main.dev.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,71 @@
"use strict";

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

var screenInput = document.querySelector("#screenInput");
var screenOutput = document.querySelector("#screenOutput"); // Special Button Functionality - Functions

function calculateAnswer() {
var regex_operators = /[+/*-]/g;
var numberArray = screenInput.innerText.split(/[+\-/*]/);

var floatArray = _toConsumableArray(numberArray).map(function (number) {
console.log(screenInput.innerText);
screenInputArray = screenInput.innerText.split(/[n+\-/*]/);
console.log(screenInputArray.indexOf(''));
screenInput.innerText.match(/[n]/) ? screenInputArray[screenInputArray.indexOf('')] = "n" : console.log("no negative numbers");
console.log(screenInputArray);
var newNumberArray = screenInputArray.filter(function (i) {
return !i.match(/[+\-/*]/);
});
console.log(newNumberArray);
var floatArray = newNumberArray.filter(function (i) {
return i.match(/[\d]/);
});
console.log(floatArray);
var floatNumbersOnly = floatArray.map(function (number) {
return parseFloat(number);
});

var whichOperatorData = screenInput.innerText.match(/[+\-/*]/g); // First switch handles the first operation with the first two numbers.

var answer = floatArray[0];

for (var i = 0; i < floatArray.length; i++) {
console.log(floatNumbersOnly);
var whichOperatorData = screenInput.innerText.match(/[+\-/*]/g);
console.log(whichOperatorData);
var answer = newNumberArray[0] === "n" ? -1 * floatNumbersOnly[0] : floatNumbersOnly[0]; // for 1 * 3
// first element is not n so answer = 1
// new numbers array = ["1", "3"]
// float array = ["1", "3"]
// which operators array = [*]
// its a multiply, so it checks next element and its not n, therefore

for (var i = 0; i <= whichOperatorData.length - 1; i++) {
switch (whichOperatorData[i]) {
case "+":
answer += floatArray[i + 1];
if (newNumberArray[i + 1] == "n") {
answer -= floatNumbersOnly[i + 1];
} else {
answer += floatNumbersOnly[i + 1];
}

break;

case "-":
answer -= floatArray[i + 1];
if (newNumberArray[i + 1] == "n") {
answer += floatNumbersOnly[i + 1];
} else {
answer -= floatNumbersOnly[i + 1];
}

break;

case "*":
answer *= floatArray[i + 1];
if (newNumberArray[i + 1] == "n") {
answer *= -1 * floatNumbersOnly[i + 1];
} else {
answer *= floatNumbersOnly[i + 1];
}

break;

case "/":
answer /= floatArray[i + 1];
if (newNumberArray[i + 1] == "n") {
answer /= floatNumbersOnly[i + 1] * -1;
} else {
answer /= floatNumbersOnly[i + 1];
}

break;
}
} // If the number is recurring, then round it.
Expand All @@ -59,6 +86,10 @@ function handleEquals(event) {
screenOutput.value = calculateAnswer().toString();
}

function handleNegativeNumber() {
screenInput.innerHTML += "n";
}

function handleDecimalInput(event) {
screenInput.innerHTML += event.target.innerHTML;
}
Expand Down Expand Up @@ -96,7 +127,9 @@ subtractButton.addEventListener("click", handleOperation);
var divideButton = document.querySelector("#divide");
divideButton.addEventListener("click", handleDivide);
var multiplyButton = document.querySelector("#multiply");
multiplyButton.addEventListener("click", handleOperation); // Basic Button Functionality and pressing animation
multiplyButton.addEventListener("click", handleOperation);
var negativeNumberButton = document.querySelector("#negativeNumber");
negativeNumberButton.addEventListener("click", handleNegativeNumber); // Basic Button Functionality and pressing animation

var allButtons = document.querySelectorAll(".buttons");
var allNumbers = document.querySelectorAll(".numbers");
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</div>
<div id = "buttons">
<button id="ac" class="buttons specialButtons" data-cy="ac">AC</button>
<button id="negativeNumber" class="buttons specialButtons" data-cy="negativeNumber">+/-</button>
<button id="delete" class="buttons specialButtons" data-cy="delete">DEL</button>
<button id="multiply" class="buttons specialButtons" data-cy="multiply">*</button>
<button id="add" class="buttons specialButtons" data-cy="add">+</button>
Expand Down
70 changes: 54 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,62 @@ let screenOutput = document.querySelector("#screenOutput");

// Special Button Functionality - Functions

function calculateAnswer() {
let regex_operators = /[+/*-]/g;
let numberArray = screenInput.innerText.split(/[+\-/*]/);
let floatArray = [...numberArray].map(number => {
return parseFloat(number);
function calculateAnswer() {
console.log(screenInput.innerText);
screenInputArray = screenInput.innerText.split(/[n+\-/*]/);
console.log(screenInputArray.indexOf(''));
screenInput.innerText.match(/[n]/) ? screenInputArray[screenInputArray.indexOf('')] = "n" : console.log("no negative numbers");
console.log(screenInputArray);
let newNumberArray = screenInputArray.filter(i => {return !i.match(/[+\-/*]/)})
console.log(newNumberArray);
let floatArray = newNumberArray.filter(i => {
return i.match(/[\d]/);
});
console.log(floatArray);
let floatNumbersOnly = floatArray.map(number => parseFloat(number));
console.log(floatNumbersOnly)
let whichOperatorData = screenInput.innerText.match(/[+\-/*]/g);
// First switch handles the first operation with the first two numbers.
let answer = floatArray[0];
for (let i=0; i<floatArray.length; i++) {
console.log(whichOperatorData)
let answer = newNumberArray[0] === "n" ? (-1*floatNumbersOnly[0]) : floatNumbersOnly[0];

// for 1 * 3
// first element is not n so answer = 1
// new numbers array = ["1", "3"]
// float array = ["1", "3"]
// which operators array = [*]
// its a multiply, so it checks next element and its not n, therefore

for (let i=0; i <= whichOperatorData.length-1; i++) {
switch (whichOperatorData[i]) {
case "+":
answer += floatArray[i+1];
case "+":
if (newNumberArray[i+1] == "n") {
answer -= floatNumbersOnly[i+1];
} else {
answer += floatNumbersOnly[i+1];
}
break;
case "-":
answer -= floatArray[i+1];
if (newNumberArray[i+1] == "n") {
answer += floatNumbersOnly[i+1];
} else {
answer -= floatNumbersOnly[i+1];
}
break;
case "*":
answer *= floatArray[i+1];
break;
if (newNumberArray[i+1] == "n") {
answer *= (-1*floatNumbersOnly[i+1]);
} else {
answer *= floatNumbersOnly[i+1];
}
break;
case "/":
answer /= floatArray[i+1];
break;
}
if (newNumberArray[i+1] == "n") {
answer /= (floatNumbersOnly[i+1]*-1);
} else {
answer /= floatNumbersOnly[i+1];
}
break;
}
}
// If the number is recurring, then round it.
regexRecurring = /\.(\d)\1+/;
Expand All @@ -44,6 +76,10 @@ function handleEquals(event) {
screenOutput.value = calculateAnswer().toString();
}

function handleNegativeNumber() {
screenInput.innerHTML += "n"
}

function handleDecimalInput(event) {
screenInput.innerHTML += event.target.innerHTML
}
Expand Down Expand Up @@ -82,6 +118,8 @@ let divideButton = document.querySelector("#divide");
divideButton.addEventListener("click", handleDivide);
let multiplyButton = document.querySelector("#multiply");
multiplyButton.addEventListener("click", handleOperation);
let negativeNumberButton = document.querySelector("#negativeNumber");
negativeNumberButton.addEventListener("click", handleNegativeNumber);



Expand Down
2 changes: 1 addition & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ body {
}

#ac {
grid-column: 1/3;
grid-column: 1/2;
}

#delete {
Expand Down
2 changes: 1 addition & 1 deletion styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ body {


#ac {
grid-column: 1 / 3;
grid-column: 1 / 2;
}

#delete {
Expand Down

0 comments on commit 25fbf65

Please sign in to comment.