What is your hint or solution suggestion?
Solution
function solve24 (numStr) {
let init_arr = numStr.split('');
let perm_arr = [init_arr];
for(let e = 0; e < 23; e++) {
// using the algorith from page 142 - https://homepage.math.uiowa.edu/~goodman/22m150.dir/2007/Permutation%20Generation%20Methods.pdf
if(e % 2 === 0) [init_arr[0], init_arr[1]] = [init_arr[1], init_arr[0]];
else if(e % 2 !== 0) {
if(e === 5 || e === 11) [init_arr[2], init_arr[3]] = [init_arr[3], init_arr[2]];
else if(e === 17) {
[init_arr[0], init_arr[3]] = [init_arr[3], init_arr[0]];
[init_arr[1], init_arr[2]] = [init_arr[2], init_arr[1]];
} else {
[init_arr[1], init_arr[2]] = [init_arr[2], init_arr[1]];
}
}
perm_arr.push(Array.from(init_arr));
};
for(let e = 0; e < perm_arr.length; e++) {
// probably these aren't all the possible formulas
let formulas = [
`(${perm_arr[e][0]}-${perm_arr[e][1]}/${perm_arr[e][2]})*${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}*${perm_arr[e][2]}*${perm_arr[e][3]}`,
`(${perm_arr[e][0]}*${perm_arr[e][1]})/(${perm_arr[e][2]}-${perm_arr[e][3]})`,
`(${perm_arr[e][3]}+${perm_arr[e][2]})*(${perm_arr[e][1]}+${perm_arr[e][0]})`,
`${perm_arr[e][0]}+${perm_arr[e][1]}+${perm_arr[e][2]}+${perm_arr[e][3]}`,
`${perm_arr[e][0]}+${perm_arr[e][1]}+${perm_arr[e][2]}-${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}*${perm_arr[e][2]}/${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}+${perm_arr[e][2]}/${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}-${perm_arr[e][2]}/${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}*${perm_arr[e][2]}+${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}*${perm_arr[e][2]}-${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}+${perm_arr[e][2]}+${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}+${perm_arr[e][2]}-${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}-${perm_arr[e][2]}-${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}+${perm_arr[e][2]}*${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}-${perm_arr[e][2]}*${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}+${perm_arr[e][2]}/${perm_arr[e][3]}`,
`${perm_arr[e][0]}*${perm_arr[e][1]}-${perm_arr[e][2]}/${perm_arr[e][3]}`,
`(${perm_arr[e][0]}+${perm_arr[e][1]})*(${perm_arr[e][2]}-${perm_arr[e][3]})`,
`(${perm_arr[e][0]}-${perm_arr[e][1]})*(${perm_arr[e][2]}+${perm_arr[e][3]})`,
`(${perm_arr[e][0]}-${perm_arr[e][1]})*(${perm_arr[e][2]}-${perm_arr[e][3]})`,
`(${perm_arr[e][0]}+${perm_arr[e][1]})/(${perm_arr[e][2]}+${perm_arr[e][3]})`,
`(${perm_arr[e][0]}+${perm_arr[e][1]})/(${perm_arr[e][2]}-${perm_arr[e][3]})`,
`(${perm_arr[e][0]}-${perm_arr[e][1]})/(${perm_arr[e][2]}+${perm_arr[e][3]})`,
`(${perm_arr[e][0]}-${perm_arr[e][1]})/(${perm_arr[e][2]}-${perm_arr[e][3]})`,
`(${perm_arr[e][0]}*${perm_arr[e][1]})/(${perm_arr[e][2]}+${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}+${perm_arr[e][2]}+${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}+${perm_arr[e][2]}-${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}-${perm_arr[e][2]}-${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}*${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}/${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}+${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}-${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}+${perm_arr[e][2]}/${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}-${perm_arr[e][2]}/${perm_arr[e][3]})`,
`${perm_arr[e][0]}/(${perm_arr[e][1]}*${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}/(${perm_arr[e][1]}/${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}/(${perm_arr[e][1]}+${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}/(${perm_arr[e][1]}-${perm_arr[e][2]}*${perm_arr[e][3]})`,
`${perm_arr[e][0]}/(${perm_arr[e][1]}+${perm_arr[e][2]}/${perm_arr[e][3]})`,
`${perm_arr[e][0]}/(${perm_arr[e][1]}-${perm_arr[e][2]}/${perm_arr[e][3]})`,
`${perm_arr[e][0]}*(${perm_arr[e][1]}-${perm_arr[e][2]}/${perm_arr[e][3]})`,
`(${perm_arr[e][0]}+${perm_arr[e][1]}) * ${perm_arr[e][2]}+${perm_arr[e][3]}`,
`(${perm_arr[e][0]}+${perm_arr[e][1]}) * ${perm_arr[e][2]}-${perm_arr[e][3]}`,
`(${perm_arr[e][0]}-${perm_arr[e][1]}) * ${perm_arr[e][2]}+${perm_arr[e][3]}`,
`(${perm_arr[e][0]}-${perm_arr[e][1]}) * ${perm_arr[e][2]}-${perm_arr[e][3]}`,
`(${perm_arr[e][0]}+${perm_arr[e][1]}) * ${perm_arr[e][2]}*${perm_arr[e][3]}`,
`(${perm_arr[e][0]}+${perm_arr[e][1]}) * ${perm_arr[e][2]}/${perm_arr[e][3]}`,
`(${perm_arr[e][0]}+${perm_arr[e][1]}) / ${perm_arr[e][2]}*${perm_arr[e][3]}`,
];
for(let i = 0; i < formulas.length; i++) {
if(eval(formulas[i]) === 24) return formulas[i];
}
}
}
console.log(solve24("4878")) ;
Challenge: 24 game
Link to the challenge: