Tell us what’s happening:
Describe your issue in detail here.
I just want to complain about this one. There are a couple of ways to do this one to spec, unfortunately the answer bank is arbitrarily limited for this question. For example, my algorithm always finds (3+(1+2))*4 instead of 3*1*4*2
because of the way I ordered my symbols. Both answers are correct, the spec doesn’t specify it wants the expression in a specific way, yet the free code camp answer bank will fail me because for whatever reason, (3+(1+2))*4 was not chosen as a correct option. Very frustrating after doing it correctly.
After some more testing, I would say that there so many missing answer, that this question is essentially broken.
7 * 8 - 4 * 8 is 24, but that will fail the test
( 3 + ( 1 + 2 ) ) * 4 is 24, but will fail the test.
6 * 8 / ( 9 - 7 ) is 24, but will fail the test
Once again, the problem doesn’t mention the expression needs to fit a specific format. So this question is basically broken.
**Your code so far**
function solve24 (numStr) {
let nums = numStr.split("").map((elem)=>{
return [elem, ""+elem];
})
let answer = false;
let ops = ['+','-','*','/']
const recursive24 = (nums) => {
if(nums.length == 1){
if (nums[0][0] == 24){
answer = nums[0][1];
}
return
}
// select first number
for(let x = 0; x < nums.length; x++){
//select a operator
for(let j = 0; j < ops.length; j++){
//select second number
for(let k = 0; k < nums.length; k++){
if (k == x){
continue
}
// copy new array, remove nums from it.
let newArr= nums.slice().filter((val, index)=>{
if (index ==k || index == x){
return false
}
return true
});
let exp = nums[x][1]+ops[j]+nums[k][1];
if (j<=1 && newArr.length > 0){
exp="("+exp+")";
}
let val = eval(exp)
newArr.push([val,exp])
recursive24(newArr)
}
}
}
}
recursive24(nums);
console.log(nums)
if(answer===false){
return "no solution exists"
}
return answer;
}
**Your browser information:**
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0
Challenge: Rosetta Code - 24 game
Link to the challenge: