# Possible solution to Rosetta Code Challenge - 24 game

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

Welcome to the forum!

``````[details]
```
code goes here...
```
[/details]
``````

Also, provide all of the necessary code to pass the challenge.

Also, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( `</>` ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.