question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

24 Game is missing answers.

See original GitHub issue

Describe the Issue

There are tons of different ways to write this algorithim, that will come up with tons of different variations of the acceptable equation that reaches 24. My algorithim came up with the following solutions: for set 4878 my algorithm returns 7 * 8 - 4 * 8 which is 24, but that will fail the test for set (1234) my algorithm returns ( 3 + ( 1 + 2 ) ) * 4 which is 24, but will fail the test. (for set (1234) and for set 6897 we get 6 * 8 / ( 9 - 7 ) which is 24, but will fail the test and 3 * 4 / 1 * 2 and i’m sure plenty more…

The question does not specify a specific equation it wants, only that it should be 24 and fit the format. So for this reason, I think its broken, and either needs its answer bank revised, or needs to have numbers with only 1 or 2 solutions chosen for the tests.

Affected Page

https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/24-game

Your code

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
    }
    if(answer){
      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(answer)
  if(answer===false){
    return "no solution exists"
  }
  return answer;
}

Expected behavior

Should pass the tests, but it fails because the answer bank does not have enough equation results.

Screenshots

No response

System

Desktop free code camp terminal

Additional context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
jeremyltcommented, Aug 5, 2022

Or we could skip all that and make a simple, lightweight expression parser.

1reaction
jeremyltcommented, Aug 5, 2022

Not really though, since we only have four operations. I’m making up a smaller parser right now

Read more comments on GitHub >

github_iconTop Results From Across the Web

24 Game - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for...
Read more >
What Is Missing 24 - Game Solver
What is the solution for What Is Missing 24 ? We are trying our best to solve the answer manually and update the...
Read more >
Is the 24 game NP-complete? - Math Stack Exchange
I'm assuming that the 24-game allows you to use rational numbers. Even if it doesn't, I think that it is possible to emulate...
Read more >
Rosetta Code - 24 game. Not enough answers - JavaScript
After some more testing, I would say that there so many missing answer, that this question is essentially broken. ... ( 3 +...
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found