I have a little calculator app.
The return statement is inside of an if statement checking for a variable to be equal to "null". If the variable is equal to "null", it should return the input the function is called with. However, it always returns undefined, no matter what the input is.
Here is the code:
const input = ['1', 'substract', '3'];
const findNextOperation = (input) => {
let operation;
let ind;
//First * or /
ind = input.findIndex(el => /multiply|divide/.test(el));
//Then + or -
if (ind === -1) ind = input.findIndex(el => /add|substract/.test(el));
//No operations left
if (ind === -1) return null;
operation = input[ind];
return { operation, ind };
}
const performOperation = (a, b, operation) => {
a = Number(a);
b = Number(b);
switch (operation) {
case 'add':
return a + b;
case 'substract':
return a - b;
case 'multiply':
return a * b;
case 'divide':
return a / b;
default:
return;
}
}
const calculate = (input) => {
const operation = findNextOperation(input);
if (operation === null) {
console.log('here', input[0]) // Logs "here -2" to the console
return input[0]; // Returns "undefined"
}
const operationOutcome = performOperation(input[operation.ind - 1], input[operation.ind + 1], operation.operation);
calculate(input.toSpliced(operation.ind - 1, 3, operationOutcome));
}
console.log(calculate(input)); // Logs "undefined" to the console
I can't figure out where it goes wrong.
Edit: I didn't know that was a thing with recursive functions. mb
You've made a recursive approach by calling calculate
in the calculate
function.
However, the return value of the second call is lost.
You should change the following
calculate(input.toSpliced(...
// to
return calculate(input.toSpliced(...
const findNextOperation = (input) => {
let operation;
let ind;
//First * or /
ind = input.findIndex(el => /multiply|divide/.test(el));
//Then + or -
if (ind === -1) ind = input.findIndex(el => /add|substract/.test(el));
//No operations left
if (ind === -1) return null;
operation = input[ind];
return { operation, ind };
}
const performOperation = (a, b, operation) => {
a = Number(a);
b = Number(b);
switch (operation) {
case 'add':
return a + b;
case 'substract':
return a - b;
case 'multiply':
return a * b;
case 'divide':
return a / b;
default:
return;
}
}
const calculate = (input) => {
const operation = findNextOperation(input);
if (operation === null) {
return input[0]; // Returns "undefined"
}
const operationOutcome = performOperation(input[operation.ind - 1], input[operation.ind + 1], operation.operation);
return calculate(input.toSpliced(operation.ind - 1, 3, operationOutcome));
}
console.log(calculate(['1', 'substract', '3']));
console.log(calculate(['10', 'divide', '3']));