24 Game test. take from it what you want, it's not by any means good, but it works!

What is your hint or solution suggestion?

Summary
function solve24 (numStr) {
  let arrOfNums = numStr.split('').map(ele => ele = Number(ele))
  let arrOfOps = ['*', '/', '+', '-']
  let count = 0
  let getRet = function(){
    let arrOfNumsClone = [...arrOfNums]
    let arrOfOpsClone = [...arrOfOps]
    let randomNum = Math.floor(Math.random() * 10)
    let retStr = ''
    let lenth = arrOfNumsClone.length
    let lenth2 = arrOfNumsClone.length
  if(randomNum > 8){retStr+='('}
  if(randomNum < 2){retStr+='('}
  retStr += arrOfNumsClone.splice(Math.floor(Math.random()*(lenth2)), 1)
  retStr += arrOfOpsClone[Math.floor(Math.random()*(lenth))]
  lenth2 = arrOfNumsClone.length
  retStr += arrOfNumsClone.splice(Math.floor(Math.random()*(lenth2)), 1)
  if(randomNum > 8){retStr+=')'}
  retStr += arrOfOpsClone[Math.floor(Math.random()*(lenth))]
  lenth2 = arrOfNumsClone.length
  if(randomNum > 8){retStr+='('}
  retStr += arrOfNumsClone.splice(Math.floor(Math.random()*(lenth2)), 1)
  if(randomNum < 2){retStr+=')'}
  retStr += arrOfOpsClone[Math.floor(Math.random()*(lenth))]
  lenth2 = arrOfNumsClone.length
  retStr += arrOfNumsClone.splice(Math.floor(Math.random()*(lenth2)), 1)
  if(randomNum > 8){retStr+=')'}
  return retStr
}
  while(count < 100000){
      let val = getRet()
     if((Function(`return ${val};`))() === 24){
       return val
     }
     count++
  }
}
console.log(solve24("1127"))

Challenge: 24 game

Link to the challenge:

Interesting approach… Since this code is, literally, random, it does not reliably pass all tests. As such, I recommend against it being included in the guide.

1 Like

Yes, of course. Plus, it takes too long to run