Bug in challenge: Rosetta Code: 24 Game
See original GitHub issueDescribe your problem and how to reproduce it:
My solution (see below) produces the correct result: For solve24("4878")
my algorithm produces (4-8+7)*(8)
which equals 24. However the automated test on this example fails - error message: solve24("4878") should return (7-8/8)*4 or 4*(7-8/8)
.
My Code:
function solve24 (numStr) {
// Generate all possible combinations of ordering the numbers
let numberCombinations = [];
for (let i1=0; i1<4; i1++) {
for (let i2=0; i2<4; i2++) {
for (let i3=0; i3<4; i3++) {
// not very elegant but short and simple ...
if (i1 !== i2 && i2 !== i3 && i1 !== i3) {
let i4 = 6 - i1 - i2 - i3;
numberCombinations.push([
numStr[i1], numStr[i2], numStr[i3], numStr[i4]
])
}
}
}
}
// Format: "_n_n_n_n_"
// Choices for symbols to fill in the gaps
const startChoices = ["", "(", "(("];
const midChoices = ["+", "-", "*", "/",
"(", ")", "((", "))", ")*(", ")/("];
const endChoices = ["", ")", "))"];
// Iterate through all number combinations
for (let i=0; i<24; i++) {
let combination = numberCombinations[i];
// Again ... not pretty but simple and stupid
for (let i1=0; i1<3; i1++) {
for (let i5=0; i5<3; i5++) {
for (let i2=0; i2<10; i2++) {
for (let i3=0; i3<10; i3++) {
for (let i4=0; i4<10; i4++) {
let mathString =
startChoices[i1] + combination[0] + midChoices[i2] +
combination[1] + midChoices[i3] + combination[2] +
midChoices[i4] +combination[3] +endChoices[i5];
try {
if (eval(mathString) == 24) {
return(mathString);
}
} catch (e) {}
}
}
}
}
}
}
return "no solution exists";
}
console.log(solve24("4878"));
Add a Link to the page with the problem: https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/24-game
Tell us about your browser and operating system:
- Browser Name: Chrome
- Browser Version: 81.0.4044.129
- Operating System: Mac OS Catalina (10.15.4)
If possible, add a screenshot here (you can drag and drop, png, jpg, gif, etc. in this box):
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Rosetta Code - 24 game. Not enough answers - JavaScript
Rosetta Code - 24 game. Not enough answers ; Tell us what's happening: Describe your issue in detail here. ; Challenge: Rosetta Code...
Read more >24 game/Solve - Rosetta Code
Write a program that takes four digits, either from user input or by random generation, and computes arithmetic expressions following the rules of...
Read more >24 game - Rosetta Code
The 24 Game tests one's mental arithmetic. Task Write a program that randomly chooses and displays four digits, each from 1 ──▻ 9...
Read more >15 puzzle game - Rosetta Code
Task. The 15-puzzle is also known as: Fifteen Puzzle; Gem Puzzle; Boss Puzzle; Game of Fifteen; Mystic Square; 14-15 Puzzle; and some others....
Read more >N-queens problem - Rosetta Code
Solve the eight queens puzzle. You can extend the problem to solve the puzzle with a board of size NxN. For the number...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Sky020 Using eval is really unsafe!
The code executed from a string is being executed with the same priviliges as if the caller executed it using regular instructions. This would be a doorway for easier XSS attacks.
See the MDN Docs for details.
@dostuffthatmatters Just made a pull request that has a checking that removes extra parentheses around single digits. I haven’t checked for situations like this though
1*2*(3*4)
where there are extra parentheses in non-single digits