I want to store the algebraic parts in an array. Currently, I have this but it's not fully working.
function exp(term) {
var container = [];
if (term[0] === '-') {
container[0] = '-1';
container[1] = term.match(/([0-9]+)/g)[0];
container[2] = term.match(/([a-zA-Z]+)/g)[0];
}
else {
container[0] = '0';
container[1] = term.match(/([0-9]+)/g)[0];
container[2] = term.match(/([a-zA-Z]+)/g)[0];
}
return container;
}
console.log(exp('-24mn')); //should output ['-1', '24', 'mn']
console.log(exp('-100BC')); //should output ['-1', '100', 'BC']
console.log(exp('100BC')); //should output ['0', '100', 'BC']
console.log(exp('100')); //should output ['0', '100', '']
console.log(exp('BC')); //should output ['0', '0', 'BC']
console.log(exp('-bC')); //should output ['-1', '0', 'bC']
console.log(exp('-100')); //should output ['-1', '100', '']
But if possible, what I really want is an array of length 2 only, containing the coefficient and the variables like:
console.log(exp('-24mn')); //should output ['-24', 'mn']
console.log(exp('-100BC')); //should output ['-100', 'BC']
console.log(exp('100BC')); //should output ['100', 'BC']
console.log(exp('100')); //should output ['100', '']
console.log(exp('BC')); //should output ['0', 'BC']
console.log(exp('-bC')); //should output ['-1', 'bC']
console.log(exp('-100')); //should output ['-100', '']
I only did the array of length 3 approach because I don't know how to deal with a case where there's only a negative sign followed by variables like '-bC' and also only variables like 'BC'. Any help will be greatly appreciated. Thanks in advance!