Skip to content

Commit

Permalink
greatly oversimplified code that was too complicated. Also fixed erro…
Browse files Browse the repository at this point in the history
…rs that occured when using two or more negative numbers in an operation. Added two cypress tests to account for multiple negative numbers
  • Loading branch information
angaaruriakhil committed Sep 24, 2021
1 parent 9d9e06b commit 8f72040
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 38 deletions.
22 changes: 22 additions & 0 deletions cypress/integration/dist/test.spec.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,26 @@ describe('Negative number functionality', function () {

cy.get('[data-cy=screenOutput]').should("have.value", '-2');
});
it('should calculate -2 + -2 and return -4', function () {
cy.visit("http://127.0.0.1:5500/index.html");
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=add]').click();
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=equals]').click(); // Assert

cy.get('[data-cy=screenOutput]').should("have.value", '-4');
});
it('should calculate -2 - -2 and return 0', function () {
cy.visit("http://127.0.0.1:5500/index.html");
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=subtract]').click();
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=equals]').click(); // Assert

cy.get('[data-cy=screenOutput]').should("have.value", '0');
});
});
22 changes: 22 additions & 0 deletions cypress/integration/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,26 @@ describe('Negative number functionality', () => {
// Assert
cy.get('[data-cy=screenOutput]').should("have.value", '-2');
})
it('should calculate -2 + -2 and return -4', () => {
cy.visit("http://127.0.0.1:5500/index.html");
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=add]').click();
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=equals]').click();
// Assert
cy.get('[data-cy=screenOutput]').should("have.value", '-4');
})
it('should calculate -2 - -2 and return 0', () => {
cy.visit("http://127.0.0.1:5500/index.html");
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=subtract]').click();
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=equals]').click();
// Assert
cy.get('[data-cy=screenOutput]').should("have.value", '0');
})
})
43 changes: 25 additions & 18 deletions dist/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,47 @@ var screenInput = document.querySelector("#screenInput");
var screenOutput = document.querySelector("#screenOutput"); // Special Button Functionality - Functions

function calculateAnswer() {
screenInputArray = screenInput.innerText.split(/[n+\-/*]/); // The split needs to allow for numbers with more than one digit. Therefore a split is used. As the split changes "n", which represents a negative number, to "", we need to switch it back to "n" in screenInputArray.
var screenInputArray = screenInput.innerText.split(/[n+\-/*]/); // The split needs to allow for numbers with more than one digit. Therefore a split is used.
// As the split changes "n", which represents a negative number, to "", we need to switch it back to "n" in screenInputArray using a map loop.

screenInput.innerText.match(/[n]/) ? screenInputArray[screenInputArray.indexOf('')] = "n" : console.log("no negative numbers"); // Let's remove all the numbers but keep special characters such as the negative number indicator "n".
var completeScreenInputArray = screenInputArray.map(function (character) {
return character == "" ? "n" : character;
}); // Let's remove all the operators but keep special characters such as the negative number indicator "n".

var filteredInputArray = screenInputArray.filter(function (i) {
var filteredInputArray = completeScreenInputArray.filter(function (i) {
return !i.match(/[+\-/*]/);
}); // Now lets grab all the numbers.
});
var floatArray = []; // Now lets grab all the numbers as floats. Use the for loop to make numbers negative based on indicator "n" if they need to be.

var numberArray = filteredInputArray.filter(function (i) {
return i.match(/[\d]/);
}); // And lets convert them into floats so we can grab any numbers with decimal points.
for (var i = 0; i < filteredInputArray.length; i++) {
if (filteredInputArray[i - 1] && filteredInputArray[i - 1] == "n") {
floatArray.push(parseFloat(filteredInputArray[i]) * -1);
} else if (filteredInputArray[i] !== "n") {
floatArray.push(parseFloat(filteredInputArray[i]));
}
}

var floatArray = numberArray.map(function (number) {
return parseFloat(number);
});
;
console.log(floatArray);
var operatorArray = screenInput.innerText.match(/[+\-/*]/g);
var answer = filteredInputArray[0] === "n" ? -1 * floatArray[0] : floatArray[0];
var answer = floatArray[0];

for (var i = 0; i <= operatorArray.length - 1; i++) {
switch (operatorArray[i]) {
for (var _i = 0; _i < floatArray.length; _i++) {
switch (operatorArray[_i]) {
case "+":
filteredInputArray[i + 1] == "n" ? answer -= floatArray[i + 1] : answer += floatArray[i + 1];
answer += floatArray[_i + 1];
break;

case "-":
filteredInputArray[i + 1] == "n" ? answer += floatArray[i + 1] : answer -= floatArray[i + 1];
answer -= floatArray[_i + 1];
break;

case "*":
filteredInputArray[i + 1] == "n" ? answer *= -1 * floatArray[i + 1] : answer *= floatArray[i + 1];
answer *= floatArray[_i + 1];
break;

case "/":
filteredInputArray[i + 1] == "n" ? answer /= floatArray[i + 1] * -1 : answer /= floatArray[i + 1];
answer /= floatArray[_i + 1];
break;
}
} // If the number is recurring, then round it.
Expand All @@ -55,7 +62,7 @@ function calculateAnswer() {
function handleEquals(event) {
screenOutput.innerText = "";
screenOutput.innerText += "= ".concat(calculateAnswer());
screenOutput.value = calculateAnswer().toString();
screenOutput.value = calculateAnswer().toString(); // for cypress tests
}

function handleNegativeNumber() {
Expand Down
45 changes: 27 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,41 @@ let screenOutput = document.querySelector("#screenOutput");
// Special Button Functionality - Functions

function calculateAnswer() {
screenInputArray = screenInput.innerText.split(/[n+\-/*]/);
// The split needs to allow for numbers with more than one digit. Therefore a split is used. As the split changes "n", which represents a negative number, to "", we need to switch it back to "n" in screenInputArray.
screenInput.innerText.match(/[n]/) ? screenInputArray[screenInputArray.indexOf('')] = "n" : console.log("no negative numbers");
// Let's remove all the numbers but keep special characters such as the negative number indicator "n".
let filteredInputArray = screenInputArray.filter(i => {return !i.match(/[+\-/*]/)})
// Now lets grab all the numbers.
let numberArray = filteredInputArray.filter(i => i.match(/[\d]/));
// And lets convert them into floats so we can grab any numbers with decimal points.
let floatArray = numberArray.map(number => parseFloat(number));
let screenInputArray = screenInput.innerText.split(/[n+\-/*]/);
// The split needs to allow for numbers with more than one digit. Therefore a split is used.
// As the split changes "n", which represents a negative number, to "", we need to switch it back to "n" in screenInputArray using a map loop.
const completeScreenInputArray = screenInputArray.map(character => {
return character == "" ? "n" : character;
});
// Let's remove all the operators but keep special characters such as the negative number indicator "n".
let filteredInputArray = completeScreenInputArray.filter(i => {return !i.match(/[+\-/*]/)});
let floatArray = [];
// Now lets grab all the numbers as floats. Use the for loop to make numbers negative based on indicator "n" if they need to be.
for (let i = 0; i<filteredInputArray.length; i++) {
if (filteredInputArray[i-1] && filteredInputArray[i-1] == "n") {
floatArray.push(parseFloat(filteredInputArray[i])*-1);
} else if (filteredInputArray[i] !== "n") {
floatArray.push(parseFloat(filteredInputArray[i]));
}
};
console.log(floatArray);
let operatorArray = screenInput.innerText.match(/[+\-/*]/g);
let answer = filteredInputArray[0] === "n" ? (-1*floatArray[0]) : floatArray[0];
let answer = floatArray[0];

for (let i=0; i <= operatorArray.length-1; i++) {
for (let i=0; i < floatArray.length; i++) {
switch (operatorArray[i]) {
case "+":
filteredInputArray[i+1] == "n" ? answer -= floatArray[i+1] : answer += floatArray[i+1];
answer += floatArray[i+1];
break;
case "-":
filteredInputArray[i+1] == "n" ? answer += floatArray[i+1] : answer -= floatArray[i+1];
case "-":
answer -= floatArray[i+1];
break;
case "*":
filteredInputArray[i+1] == "n" ? answer *= (-1*floatArray[i+1]) : answer *= floatArray[i+1];
answer *= floatArray[i+1];
break;
case "/":
filteredInputArray[i+1] == "n" ? answer /= (floatArray[i+1]*-1) : answer /= floatArray[i+1];
break;
answer /= floatArray[i+1];
break;
}
}
// If the number is recurring, then round it.
Expand All @@ -45,7 +54,7 @@ function calculateAnswer() {
function handleEquals(event) {
screenOutput.innerText = "";
screenOutput.innerText += `= ${calculateAnswer()}`;
screenOutput.value = calculateAnswer().toString();
screenOutput.value = calculateAnswer().toString(); // for cypress tests
}

function handleNegativeNumber() {
Expand Down
2 changes: 1 addition & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ body {
display: flex;
flex-direction: column;
padding: 10px;
width: 80vw;
width: 90vw;
height: 90vh;
}
}
2 changes: 1 addition & 1 deletion styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ body {
display: flex;
flex-direction: column;
padding: 10px;
width: 80vw;
width: 90vw;
height: 90vh;
}
}

0 comments on commit 8f72040

Please sign in to comment.