# Javascript calculator challenge

Hi am not able to pass the the above exercise just because of two challenges which are not working on codepen but they do work in visual studio code and cant manage to figure out why.
My codepen link is https://codepen.io/Mz12/pen/eYWYYMp?editors=0010
Generally, my calculator works by accepting inputs from the user, converts and process them. For example if the user inputs “6-1+3”, the calculator recognizes this as “6,-,1,+,3” and then process the input.
At the moment am still new to web developement and unfortunately my code is a bit to long and I dont know yet how I could shorten it. Any advice on that would be highly appreciated as well.
The following is the the javascript code that is working on visual studio code. If you would like to give it a test run please uncomment the ‘input items to process’ in order for the script to work or provide your input in the same format just note that it is still under construction though and doesnt accept all operations yet.

``````const reduceMultipleZerosAndDecimalsToOne = (input) => {
const processedInput = [];
for (let i = 0; i < input.length; i++) {
const item = input[i];

if ((item.match(/[.]/g) || []).length > 1) {
// if item has multiple decimals between numbers 5.5.5
if (item.match(/^[0-9]\.[0-9]\.[0-9]\$/)) {
// console.log(item);
// const item2 = item.replace(/[.]/g, " ").trim().replace(/\s+/, "."); // replace multiple ... with one .
const item2 = item.replace(/[.]/g, " ").trim().replace(/\s+/, "."); // replace multiple ... with one .
const item3 = item2.replace(/\s+/, ""); // replace remaning whitespace
// console.log(item3);
if (item3) {
processedInput.push(item3);
}
} else {
//if item has multiple decimals between numbers eg 5...55
// console.log(item);
const item2 = item.replace(/[.]/g, " ").trim().replace(/\s+/, "."); // replace multiple ... with one .
// console.log(item2);
if (item2) {
processedInput.push(item2);
}
}
} else if (item.match(/^0+/g)) {
const item2 = item.replace(/^0+/g, 0); // replace multiple 0ss before a number to one zero
if (item2) {
processedInput.push(Number(item2));
//   console.log(item2);
}
} else if (Number(item) == 0) {
const item2 = Number(item); // convert multiple 0s to one 0
if (item2) {
processedInput.push(item2);
//   console.log(item2);
}
} else {
processedInput.push(item);
// console.log(item);
}
}
return processedInput;
};

let regex1;
let regex2;
let unWanted;
let wanted;

// a function to make input to be -X if preceded by - sign after /X+-
const correctFormatNegativeNumbers = (input, clickedMethods) => {
// 3,+,5,X,6,-,2,/,4
const regex1 = /[0-9],[\/|X|+|-],-,[0-9]/g; // test if input has negative number and is preceded with /X+-
const regex2 = /^(-,[0-9],[\/|X|+|-],[0-9])/g; // test if input has negative number and is followed with /X+-
// const regex2 = /-,[0-9],[\/|X|+|-],[0-9]/g; // test if input has negative number and is followed with /X+-
const regex3 = /^(-,[0-9],[\/|X|+|-](.*?))/g; // test if input has a starting negative number and is followed with /X+- then anything
const regex4 = /((.*?)[\/|X|+|-],-,[0-9](.*?))/g; // test if input has negative number theat is preceded with anyhting and /X+- and is followed with /X+-

if (regex3.test(input) || regex4.test(input)) {
// console.log("Regex is regex3 || regex4");
// console.log(input.split(","));
const unWanted1 = "-,";
const wanted1 = "-";
const unWanted2 = ",-,";
const wanted2 = ",-";

const input2 = input
.slice()
.toString()
.replace(unWanted1, wanted1)
.replace(unWanted2, wanted2);

//drop - from methods
const newMethods = input2
.replace(/[0-9]|-[0-9]/g, "")
.replace(/,-,/g, ",")
.replace(/-,/g, "");
// console.log(newMethods);

const processedItems = [input2.split(","), newMethods];
// const processedItems = input2;
return processedItems;

// change -, input to -input
} else if (regex1.test(input)) {
// console.log("Regex is regex1");
// console.log(input.split(","));
const unWanted = ",-,";
const wanted = ",-";

const input2 = input.slice().toString().replace(unWanted, wanted);
// console.log(input2);

//drop - from methods
const newMethods = input2
.replace(/[0-9]|-[0-9]/g, "")
.replace(/,-,/g, ",")
.replace(/-,/g, "");
// console.log(newMethods);

const processedItems = [input2.toString().split(","), newMethods];
// const processedItems = input2;
return processedItems;

// change -, input to -input
} else if (regex2.test(input)) {
// console.log("Regex is regex2");
const unWanted = "-,";
const wanted = "-";

const input2 = input.slice().toString().replace(unWanted, wanted);
// console.log(input2);

//drop - from methods
const newMethods = input2
.replace(/[0-9]|-[0-9]/g, "")
.replace(/,-,/g, ",")
.replace(/-,/g, "");
// console.log(newMethods);

const processedItems = [input2.split(","), newMethods];
// const processedItems = input2;
return processedItems;

// change -, input to -input
} else if (
regex1.test(input) == false ||
regex2.test(input) == false ||
regex3.test(input) == false ||
regex4.test(input) == false
) {
// console.log(input + " doesnt have regex");
// console.log(input);
const processedItems = [input.toString().split(","), clickedMethods];
// const processedItems = input.split(",");
return processedItems;
}
};

//     getpreviousAndNextNumbers of an operation
const getpreviousAndNextNumbers = (currentMethod, bodmasNumber) => {
const currentMethodPosition = bodmasNumber.indexOf(currentMethod);
// console.log("currentMethodPosition is " + currentMethodPosition);

if (
bodmasNumber.length == 5 &&
bodmasNumber.includes("+") &&
bodmasNumber.includes("-") &&
bodmasNumber.indexOf("+") > bodmasNumber.indexOf("-")
) {
const previousNumber = bodmasNumber[0];
const nextNumber = bodmasNumber.slice(
currentMethodPosition + 1,
bodmasNumber.length
);
const CollectedNumbers = previousNumber + "," + nextNumber;
return CollectedNumbers;
//   }
} else {
// otherwise obtain index as usual
// console.log(bodmasNumber + " has no regex");
// get index for previous number
const previousNumberIndex = currentMethodPosition - 1;
// get previous number
const previousNumber = bodmasNumber[previousNumberIndex];
// get index for next number
const nextNumberIndex = currentMethodPosition + 1;
// get next number
const nextNumber = bodmasNumber[nextNumberIndex];
// const nextNumber = '';

// combine previous and next numbers
const CollectedNumbers = previousNumber + "," + nextNumber;
return CollectedNumbers;
}
};

let inputtPreviousAndNextNumbers;
let inputmethod;
let inputFirstNumber;
let inputSecondNumber;
let collectedMethods;

//     perform /, *, + or - operation
const performOperation = (inputmethod, inputFirstNumber, inputSecondNumber) => {
if ("/".valueOf() == inputmethod.valueOf()) {
const product = Number(inputFirstNumber) / Number(inputSecondNumber);
return product;
} else if ("X".valueOf() == inputmethod.valueOf()) {
const product = Number(inputFirstNumber) * Number(inputSecondNumber);
return product;
} else if ("+".valueOf() == inputmethod.valueOf()) {
const product = Number(inputFirstNumber) + Number(inputSecondNumber);
return product;
} else if ("-".valueOf() == inputmethod.valueOf()) {
const product = Number(inputFirstNumber) - Number(inputSecondNumber);
return product;
}
};

//  get previous and next items in the input after bodmas
let currentOperation;
let itemsInOperation;

const getRemainingItems = (currentOperation, itemsInOperation) => {
const currentOperationPosition = itemsInOperation.indexOf(currentOperation);

if (
// if input has - sign before + sign eg 6-1+3, process it here
itemsInOperation.length == 5 &&
itemsInOperation.includes("+") &&
itemsInOperation.includes("-") &&
itemsInOperation.indexOf("+") > itemsInOperation.indexOf("-")
) {
const previousItems = itemsInOperation.slice(1, currentOperationPosition);
const nextItems = "";
const remainingItems = previousItems + nextItems;
return remainingItems;

//   }
} else if (currentOperationPosition == 1 && inputMethods.length <= 3) {
const previousItems = "";
const nextItems = "";
const remainingItems = previousItems + nextItems;
return remainingItems;
} else if (inputMethods.length > 3) {
if (currentOperationPosition == 1) {
// console.log("regex is condition 3");
const previousItems = "";
const nextItems = itemsInOperation.slice(
itemsInOperation.indexOf(currentOperation) + 2,
itemsInOperation.length
);
const remainingItems = previousItems + "," + nextItems;
return remainingItems;
} else if (currentOperationPosition == itemsInOperation.length - 2) {
const previousItems = itemsInOperation.slice(
0,
itemsInOperation.length - 3
);
const nextItems = "";
const remainingItems = previousItems + "," + nextItems;
return remainingItems;
} else {
const previousmethodPosition = currentOperationPosition - 2;
const nextMethodPosition = currentOperationPosition + 2;
const previousItems = itemsInOperation.slice(
0,
previousmethodPosition + 1
);
const nextItems = itemsInOperation.slice(nextMethodPosition);
const remainingItems = previousItems + "," + nextItems;
return remainingItems;
}
}
};

let remainingItems;
let operationProduct;

//     join product and remaining items
const addProductToRemainingItems = (
currentOperation,
itemsInOperation,
remainingItems,
operationProduct
) => {
const currentOperationPosition = itemsInOperation.indexOf(currentOperation);
if (
// if input has - sign before + sign eg 6-1+3, process it here
itemsInOperation.length == 5 &&
itemsInOperation.includes("+") &&
itemsInOperation.includes("-") &&
itemsInOperation.indexOf("+") > itemsInOperation.indexOf("-")
) {
const regeneratedItems = operationProduct + "," + remainingItems;
return regeneratedItems;

//   }
} else if (currentOperationPosition == 1) {
const regeneratedItems = operationProduct + remainingItems;
return regeneratedItems;
} else if (currentOperationPosition == itemsInOperation.length - 2) {
const regeneratedItems = remainingItems + operationProduct;
return regeneratedItems;
} else {
const previousmethodPosition = currentOperationPosition - 1;
const nextMethodPosition = currentOperationPosition + 2;
const previousItems = itemsInOperation.slice(0, previousmethodPosition);
const nextItems = itemsInOperation.slice(nextMethodPosition);
const regeneratedItems =
previousItems + "," + operationProduct + "," + nextItems;
return regeneratedItems;
}
};

////////////////////////////////////////////////////////////////////////
// input items to process

// input A
// const itemsToProcess = "5,X,-,5"; // before the splitting;
// const method = ",X,-,";

// input B
// const itemsToProcess = "6,-,1,+,3"; // before the splitting;
// const method = ",-,,+,";

// input C
// const itemsToProcess = "3,+,5,X,6,-,2"; // before the splitting;
// const method = ",+,,X,,-,";

// input D
// const itemsToProcess = "3,+,5,X,6,-,2,/,4"; // input items before the splitting;
// const method = ",+,,X,,-,,/,";

// process input items
const itemsToProcessSplitted = itemsToProcess.split(",");

//   reduce multiple 0s or .s to one 0 or .
const processedInputs1 = reduceMultipleZerosAndDecimalsToOne(
itemsToProcessSplitted
);

//  make input to be -input if preceded by - sign
const processedInputs2 = correctFormatNegativeNumbers(
processedInputs1,
method
)[0];

// reformat the clicked method to get rid of the above - sign
const processedMethods = correctFormatNegativeNumbers(
processedInputs1,
method
)[1];

//  reset input items
const InpuItemsToProcess = processedInputs2;
// console.log("InpuItemsToProcess " + InpuItemsToProcess);

// reset clicked methods to mirror removed - sign
// setMethod("");
// setMethod((method) => method + processedMethods);

// splits clicked methods
const inputMethods = processedMethods.split(",");
// console.log("inputMethods " + inputMethods);

/////////////////////////////////////////////////////////////////////////////
// option A: execute the above functions based on either division, multiplication,
// addition and subtraction only
if (
(inputMethods.includes("/") ||
inputMethods.includes("X") ||
inputMethods.includes("+") ||
inputMethods.includes("-")) &&
inputMethods.length == 3
) {
if (inputMethods.includes("/")) {
const previousAndNextNumbers = getpreviousAndNextNumbers(
"/",
InpuItemsToProcess
);
const firstNumber = previousAndNextNumbers.split(",")[0];
const secondNumber = previousAndNextNumbers.split(",")[1];
const returnedProduct = performOperation("/", firstNumber, secondNumber);

console.log(returnedProduct);
} else if (inputMethods.includes("X")) {
const previousAndNextNumbers = getpreviousAndNextNumbers(
"X",
InpuItemsToProcess
);
const firstNumber = previousAndNextNumbers.split(",")[0];
const secondNumber = previousAndNextNumbers.split(",")[1];
const returnedProduct = performOperation("X", firstNumber, secondNumber);
console.log(returnedProduct);
} else if (inputMethods.includes("+")) {
const previousAndNextNumbers = getpreviousAndNextNumbers(
"+",
InpuItemsToProcess
);
const firstNumber = previousAndNextNumbers.split(",")[0];
const secondNumber = previousAndNextNumbers.split(",")[1];
const returnedProduct = performOperation("+", firstNumber, secondNumber);
console.log(returnedProduct);
} else if (inputMethods.includes("-")) {
const previousAndNextNumbers = getpreviousAndNextNumbers(
"-",
InpuItemsToProcess
);
const firstNumber = previousAndNextNumbers.split(",")[0];
const secondNumber = previousAndNextNumbers.split(",")[1];
const returnedProduct = performOperation("-", firstNumber, secondNumber);
console.log(returnedProduct);
}
}

//////////////////////////////////////////////////////////////
// option B:  bodmas on addittion and subtraction
else if (
inputMethods.includes("+") &&
inputMethods.includes("-") &&
inputMethods.length == 5
) {
// bodmas on addition and subtraction
const additionPreviousAndNextNumbers = getpreviousAndNextNumbers(
"+",
InpuItemsToProcess
);

// console.log(
// );
const additionProduct = performOperation(
"+",
);

// subtraction
const additionOtherRemainingItems = getRemainingItems(
"+",
InpuItemsToProcess
);

"+",
InpuItemsToProcess,
);
const subtractionPreviousAndNextNumbers = getpreviousAndNextNumbers(
"-",
);
const subtractionFirstNumber =
subtractionPreviousAndNextNumbers.split(",")[0];
const subtractionSecondNumber =
subtractionPreviousAndNextNumbers.split(",")[1];
const subtractionProduct = performOperation(
"-",
subtractionFirstNumber,
subtractionSecondNumber
);
// console.log("subtractionProduct " + subtractionProduct);
console.log(subtractionProduct);
}

//////////////////////////////////////////////////////////////////////
// option C:  bodmas on all operations except division
else if (
// this works here but not in the script
inputMethods.includes("X") &&
inputMethods.includes("+") &&
inputMethods.includes("-") &&
inputMethods.length == 7
) {
//  multiplication
const multiplicationPreviousAndNextNumbers = getpreviousAndNextNumbers(
"X",
InpuItemsToProcess
);
// console.log(
// "multiplicationPreviousAndNextNumbers " +
// multiplicationPreviousAndNextNumbers
// );
const multiplicationFirstNumber =
multiplicationPreviousAndNextNumbers.split(",")[0];
const multiplicationSecondNumber =
multiplicationPreviousAndNextNumbers.split(",")[1];
const multiplicationProduct = performOperation(
"X",
multiplicationFirstNumber,
multiplicationSecondNumber
);
// console.log("multiplicationProduct " + multiplicationProduct);

const multiplicationOtherRemainingItems = getRemainingItems(
"X",
InpuItemsToProcess
);

// console.log(
//   "multiplicationOtherRemainingItems " + multiplicationOtherRemainingItems
// );

"X",
InpuItemsToProcess,
multiplicationOtherRemainingItems,
multiplicationProduct
);

// console.log(
// );

const addittionPreviousAndNextNumbers = getpreviousAndNextNumbers(
"+",
);

// console.log(
// );

// );
const additionProduct = performOperation(
"+",
);

// subtraction
const additionOtherRemainingItems = getRemainingItems(
"+",
);

"+",
);

const subtractionPreviousAndNextNumbers = getpreviousAndNextNumbers(
"-",
);

// console.log(
//   "subtractionPreviousAndNextNumbers " + subtractionPreviousAndNextNumbers
// );

const subtractionFirstNumber =
subtractionPreviousAndNextNumbers.split(",")[0];
const subtractionSecondNumber =
subtractionPreviousAndNextNumbers.split(",")[1];
const subtractionProduct = performOperation(
"-",
subtractionFirstNumber,
subtractionSecondNumber
);

// console.log("subtractionProduct " + subtractionProduct);
console.log(subtractionProduct);
}

//////////////////////////////////////////////////////////////
// option D:bodmas on all operations
else if (
inputMethods.includes("/") &&
inputMethods.includes("X") &&
inputMethods.includes("+") &&
inputMethods.includes("-") &&
inputMethods.length == 9
) {
const divisionPreviousAndNextNumbers = getpreviousAndNextNumbers(
"/",
InpuItemsToProcess
);
const divisionFirstNumber = divisionPreviousAndNextNumbers.split(",")[0];
const divisionSecondNumber = divisionPreviousAndNextNumbers.split(",")[1];
const divisionProduct = performOperation(
"/",
divisionFirstNumber,
divisionSecondNumber
);
const divisionOtherRemainingItems = getRemainingItems(
"/",
InpuItemsToProcess
);

// console.log("divisionOtherRemainingItems " + divisionOtherRemainingItems);
"/",
InpuItemsToProcess,
divisionOtherRemainingItems,
divisionProduct
);

// multiplication
const multiplicationPreviousAndNextNumbers = getpreviousAndNextNumbers(
"X",
);

// console.log(
//   "multiplicationPreviousAndNextNumbers " +
//     multiplicationPreviousAndNextNumbers
// );
const multiplicationFirstNumber =
multiplicationPreviousAndNextNumbers.split(",")[0];
const multiplicationSecondNumber =
multiplicationPreviousAndNextNumbers.split(",")[1];

const multiplicationProduct = performOperation(
"X",
multiplicationFirstNumber,
multiplicationSecondNumber
);
const multiplicationOtherRemainingItems = getRemainingItems(
"X",
);
"X",
multiplicationOtherRemainingItems,
multiplicationProduct
);
const addittionPreviousAndNextNumbers = getpreviousAndNextNumbers(
"+",
);
const additionProduct = performOperation(
"+",
);

// subtraction
const additionOtherRemainingItems = getRemainingItems(
"+",
);
"+",
);
const subtractionPreviousAndNextNumbers = getpreviousAndNextNumbers(
"-",
);
const subtractionFirstNumber =
subtractionPreviousAndNextNumbers.split(",")[0];
const subtractionSecondNumber =
subtractionPreviousAndNextNumbers.split(",")[1];
const subtractionProduct = performOperation(
"-",
subtractionFirstNumber,
subtractionSecondNumber
);

console.log(subtractionProduct);
// console.log("subtractionProduct " + subtractionProduct);