1
var diffWaysToCompute = function (expression) {2
// We can iterate through the expression character-by-character.3
// we can break the expression into two halves whenever we get an operator (+, -, *).4
// The two parts can be calculated by recursively calling the function.5
// Once we have the evaluation results from the left and right halves, we can combine them to produce all results.7
return diffWaysRec({}, expression);10
function diffWaysRec(map, expression) {11
if (expression in map) {12
return map[expression];17
!expression.includes("+") &&18
!expression.includes("-") &&19
!expression.includes("*")21
result.push(parseInt(expression));23
for (let i = 0; i < expression.length; i++) {24
let char = expression[i];26
if (isNaN(parseInt(char, 10))) {27
//not a number , must it expression28
//calculate lest and right now29
let leftParts = diffWaysRec(map, expression.substring(0, i));30
let rightParts = diffWaysRec(map, expression.substring(i + 1));31
for (let l = 0; l < leftParts.length; l++) {32
for (let r = 0; r < rightParts.length; r++) {33
let left = leftParts[l];34
let right = rightParts[r];37
result.push(parseInt(left) + parseInt(right));40
result.push(parseInt(left) - parseInt(right));43
result.push(parseInt(left) * parseInt(right));51
map[expression] = result;