What is your hint or solution suggestion?
Summary
function solve24 (numStr) {
var operands = numStr.split('').sort((a, b) => {return b - a});
var operators = ['+', '-', '/', '*'];
var perms = [];
var perms0 = operands.map((n) => {return [n, parseInt(n)];});
var perms2 = [];
for(var i = 0; i < 4; i++){
for(var j = i+1; j < 4; j++){
for(var k = 0; k < 4; k++){
perms.push(evaluate(operands[i], operators[k], operands[j]));
}
}
}
perms = perms.sort((a, b) => {return b[1] - a[1];});
for(var i = 0; i < perms.length; i++){
for(var j = 0; j < 4; j++){
for(var k = 0; k < 4; k++){
if(perms[i][1] > operands[j])
{perms2.push(evaluate2(perms[i], operators[k], operands[j]));}
else
{perms2.push(evaluate2(operands[j], operators[k], perms[i]));}
}
}
}
//check 1 = (exp1)op(exp2)
for(var i = 0; i < perms.length-1; i++){
for(var j = i+1; j < perms.length; j++){
numsarr = [perms[i][0].split('').filter((num) => {return !isNaN(num)})[0]*1,
perms[i][0].split('').filter((num) => {return !isNaN(num)})[1]*1,
perms[j][0].split('').filter((num) => {return !isNaN(num)})[0]*1,
perms[j][0].split('').filter((num) => {return !isNaN(num)})[1]*1]
.sort((a, b) => {return b - a});
if(numsarr.reduce((str, value) => { return str + '_' + value;}) == operands.reduce((str, value) => { return str + '_' + value;})){
if(perms[i][1] * perms[j][1] == 24){
return perms[i][0] + '*' + perms[j][0];
}
if((perms[i][1]*1) + (perms[j][1]*1) == 24){
return perms[i][0] + '+' + perms[j][0];
}
if((perms[i][1]*1) / (perms[j][1]*1) == 24){
return perms[i][0] + '/' + perms[j][0];
}
if((perms[j][1]*1) / (perms[i][1]*1) == 24){
return perms[j][0] + '/' + perms[i][0];
}
}
}
}
for(var i = 0; i < perms2.length; i++){
for(var j = 0; j < perms0.length; j++){
numsarr = [perms2[i][0].split('').filter((num) => {return !isNaN(num)})[0]*1,
perms2[i][0].split('').filter((num) => {return !isNaN(num)})[1]*1,
perms2[i][0].split('').filter((num) => {return !isNaN(num)})[2]*1,
perms0[j][0].split('')[0]*1]
.sort((a, b) => {return b - a});
if(numsarr.reduce((str, value) => { return str + '_' + value;}) == operands.reduce((str, value) => { return str + '_' + value;})){
if(perms2[i][1] * perms0[j][1] == 24){
return '(' + perms2[i][0] + ')*' + perms0[j][0];
}
if((perms2[i][1]*1) + (perms0[j][1]*1) == 24){
return '(' + perms2[i][0] + ')+' + perms0[j][0];
}
if((perms2[i][1]*1) / (perms0[j][1]*1) == 24){
return '(' + perms2[i][0] + ')/' + perms0[j][0];
}
if((perms0[j][1]*1) / (perms2[i][1]*1) == 24){
return perms0[j][0] + '/(' + perms2[i][0] + ')';
}
}
}
}
return "no solution exists";
}
function evaluate(a, op, b){
if(op == '+') return ["("+a+op+b+")", parseInt(a)+parseInt(b)];
else if(op == '-') return ["("+a+op+b+")", a-b];
else if(op == '/') return ["("+a+op+b+")", a/b];
else if(op == '*') return ["("+a+op+b+")", a*b];
}
function evaluate2(a, op, b){
if(op == '+') return [(Array.isArray(a)?a[0]:a)+op+(Array.isArray(b)?b[0]:b), parseInt((Array.isArray(a)?a[1]:a))+parseInt(Array.isArray(b)?b[1]:b)];
else if(op == '-') return [(Array.isArray(a)?a[0]:a)+op+(Array.isArray(b)?b[0]:b), (Array.isArray(a)?a[1]:a)-(Array.isArray(b)?b[1]:b)];
else if(op == '/') return [(Array.isArray(a)?a[0]:a)+op+(Array.isArray(b)?b[0]:b), (Array.isArray(a)?a[1]:a)/(Array.isArray(b)?b[1]:b)];
else if(op == '*') return [(Array.isArray(a)?a[0]:a)+op+(Array.isArray(b)?b[0]:b), (Array.isArray(a)?a[1]:a)*(Array.isArray(b)?b[1]:b)];
}
Challenge: 24 game
Link to the challenge: