Skip to content

Commit

Permalink
fixed variable names to be more accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
angaaruriakhil committed Sep 21, 2021
1 parent 25fbf65 commit 27135ae
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 93 deletions.
47 changes: 47 additions & 0 deletions cypress/integration/dist/test.spec.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,51 @@ describe('Special operators and other functionality', function () {

cy.get('[data-cy=screenOutput]').should("have.value", '6');
});
});
describe('Negative number functionality', function () {
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=add]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=equals]').click(); // Assert

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

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

cy.get('[data-cy=screenOutput]').should("have.value", '-2');
});
});
48 changes: 48 additions & 0 deletions cypress/integration/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,52 @@ describe('Special operators and other functionality', () => {
// Assert
cy.get('[data-cy=screenOutput]').should("have.value", '6');
})
})

describe('Negative number functionality', () => {
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=add]').click();
cy.get('[data-cy=two]').click();
cy.get('[data-cy=equals]').click();
// Assert
cy.get('[data-cy=screenOutput]').should("have.value", '0');
})
it('should calculate -1000-500 and return -1500', () => {
cy.visit("http://127.0.0.1:5500/index.html");
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=one]').click();
cy.get('[data-cy=zero]').click();
cy.get('[data-cy=zero]').click();
cy.get('[data-cy=zero]').click();
cy.get('[data-cy=subtract]').click();
cy.get('[data-cy=five]').click();
cy.get('[data-cy=zero]').click();
cy.get('[data-cy=zero]').click();
cy.get('[data-cy=equals]').click();
// Assert
cy.get('[data-cy=screenOutput]').should("have.value", '-1500');
})
it('should calculate 2- -2 and return 4', () => {
cy.visit("http://127.0.0.1:5500/index.html");
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", '4');
})
it('should calculate 2+ -4 and return -2', () => {
cy.visit("http://127.0.0.1:5500/index.html");
cy.get('[data-cy=two]').click();
cy.get('[data-cy=add]').click();
cy.get('[data-cy=negativeNumber]').click();
cy.get('[data-cy=four]').click();
cy.get('[data-cy=equals]').click();
// Assert
cy.get('[data-cy=screenOutput]').should("have.value", '-2');
})
})
68 changes: 20 additions & 48 deletions dist/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,40 @@ var screenInput = document.querySelector("#screenInput");
var screenOutput = document.querySelector("#screenOutput"); // Special Button Functionality - Functions

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);
var newNumberArray = screenInputArray.filter(function (i) {
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".

var filteredInputArray = screenInputArray.filter(function (i) {
return !i.match(/[+\-/*]/);
});
console.log(newNumberArray);
var floatArray = newNumberArray.filter(function (i) {
}); // Now lets grab all the numbers.

var numberArray = filteredInputArray.filter(function (i) {
return i.match(/[\d]/);
});
console.log(floatArray);
var floatNumbersOnly = floatArray.map(function (number) {
}); // And lets convert them into floats so we can grab any numbers with decimal points.

var floatArray = numberArray.map(function (number) {
return parseFloat(number);
});
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 "+":
if (newNumberArray[i + 1] == "n") {
answer -= floatNumbersOnly[i + 1];
} else {
answer += floatNumbersOnly[i + 1];
}
var operatorArray = screenInput.innerText.match(/[+\-/*]/g);
var answer = filteredInputArray[0] === "n" ? -1 * floatArray[0] : floatArray[0];

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

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

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

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

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

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

filteredInputArray[i + 1] == "n" ? answer /= floatArray[i + 1] * -1 : answer /= floatArray[i + 1];
break;
}
} // If the number is recurring, then round it.
Expand Down
62 changes: 17 additions & 45 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,31 @@ let screenOutput = document.querySelector("#screenOutput");
// Special Button Functionality - Functions

function calculateAnswer() {
console.log(screenInput.innerText);
screenInputArray = screenInput.innerText.split(/[n+\-/*]/);
console.log(screenInputArray.indexOf(''));
// 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");
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);
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]) {
// 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 operatorArray = screenInput.innerText.match(/[+\-/*]/g);
let answer = filteredInputArray[0] === "n" ? (-1*floatArray[0]) : floatArray[0];

for (let i=0; i <= operatorArray.length-1; i++) {
switch (operatorArray[i]) {
case "+":
if (newNumberArray[i+1] == "n") {
answer -= floatNumbersOnly[i+1];
} else {
answer += floatNumbersOnly[i+1];
}
filteredInputArray[i+1] == "n" ? answer -= floatArray[i+1] : answer += floatArray[i+1];
break;
case "-":
if (newNumberArray[i+1] == "n") {
answer += floatNumbersOnly[i+1];
} else {
answer -= floatNumbersOnly[i+1];
}
filteredInputArray[i+1] == "n" ? answer += floatArray[i+1] : answer -= floatArray[i+1];
break;
case "*":
if (newNumberArray[i+1] == "n") {
answer *= (-1*floatNumbersOnly[i+1]);
} else {
answer *= floatNumbersOnly[i+1];
}
break;
filteredInputArray[i+1] == "n" ? answer *= (-1*floatArray[i+1]) : answer *= floatArray[i+1];
break;
case "/":
if (newNumberArray[i+1] == "n") {
answer /= (floatNumbersOnly[i+1]*-1);
} else {
answer /= floatNumbersOnly[i+1];
}
filteredInputArray[i+1] == "n" ? answer /= (floatArray[i+1]*-1) : answer /= floatArray[i+1];
break;
}
}
Expand Down

0 comments on commit 27135ae

Please sign in to comment.