--- title: 24 game id: 5951e88f64ebf159166a1176 challengeType: 5 --- ## Description
The 24 Game tests a person's mental arithmetic. The aim of the game is to arrange four numbers in a way that when evaluated, the result is 24
## Instructions
Implement a function that takes a string of four digits as its argument, with each digit from 1 to 9 (inclusive) with repetitions allowed, and returns an arithmetic expression that evaluates to the number 24. If no such solution exists, return "no solution exists".

Rules:

| Example input | Example output | | --- | --- | | solve24("4878"); | (7-8/8)*4 | | solve24("1234"); | 3*1*4*2 | | solve24("6789"); | (6*8)/(9-7) | | solve24("1127"); | (1+7)*(2+1) |
## Tests
```yml tests: - text: solve24 is a function. testString: assert(typeof solve24 === 'function', 'solve24 is a function.'); - text: solve24("4878") should return (7-8/8)*4 or 4*(7-8/8) testString: assert(include(answers[0], solve24(testCases[0])), 'solve24("4878") should return (7-8/8)*4 or 4*(7-8/8)'); - text: solve24("1234") should return any arrangement of 1*2*3*4 testString: assert(include(answers[1], solve24(testCases[1])), 'solve24("1234") should return any arrangement of 1*2*3*4'); - text: solve24("6789") should return (6*8)/(9-7) or (8*6)/(9-7) testString: assert(include(answers[2], solve24(testCases[2])), 'solve24("6789") should return (6*8)/(9-7) or (8*6)/(9-7)'); - text: solve24("1127") should return a permutation of (1+7)*(1*2) testString: assert(include(answers[3], solve24(testCases[3])), 'solve24("1127") should return a permutation of (1+7)*(1*2)'); ```
## Challenge Seed
```js function solve24(numStr) { // Good luck! return true; } ```
### After Test
```js const testCases = [ '4878', '1234', '6789', '1127' ]; const answers = [ ['(7-8/8)*4', '4*(7-8/8)', '(4-8+7)*8', '(4+7-8)*8', '(7+4-8)*8', '(7-8+4)*8', '8*(4-8+7)', '8*(4+7-8)', '8*(7+4-8)', '8*(7-8+4)'], ['1*2*3*4', '1*2*4*3', '1*3*2*4', '1*3*4*2', '1*4*2*3', '1*4*3*2', '2*1*3*4', '2*1*4*3', '2*3*1*4', '2*3*4*1', '2*4*3*1', '2*4*1*3', '3*1*2*4', '3*1*4*2', '3*2*1*4', '3*2*4*1', '3*4*1*2', '3*4*2*1', '4*1*2*3', '4*1*3*2', '4*2*1*3', '4*2*3*1', '4*3*1*2', '4*3*2*1', '(1+2+3)*4', '(1+3+2)*4', '(2+1+3)*4', '(2+3+1)*4', '(3+1+2)*4', '(3+2+1)*4', '4*(1+2+3)', '4*(2+1+3)', '4*(2+3+1)', '4*(3+1+2)', '4*(3+2+1)'], ['(6*8)/(9-7)', '(8*6)/(9-7)', '6*8/(9-7)', '8*6/(9-7)'], ['(1+7)*(2+1)', '(1+7)*(1+2)', '(1+2)*(1+7)', '(1+2)*(7+1)', '(2+1)*(1+7)', '(7+1)*(2+1)'] ]; function include(ansArr, res) { const index = ansArr.indexOf(res); return index >= 0; } ```
## Solution
```js // noprotect function solve24(numStr) { const digitsArr = numStr.split(''); const answers = []; const digitPermutations = []; const operatorPermutations = []; function generateDigitPermutations (digits, permutations = []) { if (digits.length === 0) { digitPermutations.push(permutations); } else { for (let i = 0; i < digits.length; i++) { const curr = digits.slice(); const next = curr.splice(i, 1); generateDigitPermutations(curr.slice(), permutations.concat(next)); } } } function generateOperatorPermutations (permutations = []) { const operators = ['+', '-', '*', '/']; if (permutations.length === 3) { operatorPermutations.push(permutations); } else { for (let i = 0; i < operators.length; i++) { const curr = permutations.slice(); curr.push(operators[i]); generateOperatorPermutations(curr); } } } generateDigitPermutations(digitsArr); generateOperatorPermutations(); interleave(); return answers[0]; function interleave () { for (let i = 0; i < digitPermutations.length; i++) { for (let j = 0; j < operatorPermutations.length; j++) { const d = digitPermutations[i]; const o = operatorPermutations[j]; const perm = [ `${d[0]}${o[0]}${d[1]}${o[1]}${d[2]}${o[2]}${d[3]}`, `(${d[0]}${o[0]}${d[1]})${o[1]}${d[2]}${o[2]}${d[3]}`, `${d[0]}${o[0]}(${d[1]}${o[1]}${d[2]})${o[2]}${d[3]}`, `${d[0]}${o[0]}${d[1]}${o[1]}(${d[2]}${o[2]}${d[3]})`, `${d[0]}${o[0]}(${d[1]}${o[1]}${d[2]}${o[2]}${d[3]})`, `(${d[0]}${o[0]}${d[1]}${o[1]}${d[2]})${o[2]}${d[3]}`, `(${d[0]}${o[0]}${d[1]})${o[1]}(${d[2]}${o[2]}${d[3]})` ]; perm.forEach(combination => { const res = eval(combination); if (res === 24) { return answers.push(combination); } }); } } } } ```