Skip to content

Instantly share code, notes, and snippets.

@darkcris1
Created January 3, 2021 11:23
Show Gist options
  • Save darkcris1/efcb46ecbbb8ee8a8d35589a9583076d to your computer and use it in GitHub Desktop.
Save darkcris1/efcb46ecbbb8ee8a8d35589a9583076d to your computer and use it in GitHub Desktop.
Find the unknown digit | Codewars
function solveExpression(exp) {
for (let i = 0; i <= 9; i++) {
const ex = exp.replace(/\?/g, i).split('=')
if (/--/.test(ex[0])) {
ex[0] = ex[0].replace(/(-([0-9]+))/g, '($1)')
}
if (/\b00|00\b/g.test(ex.join('='))) continue
if (exp.includes(i)) continue
if (eval(ex[0]) == ex[1]) return i
}
return -1
}
console.log(solveExpression('123?45*?=100')) // 0
@Abdallah-50
Copy link

thanks this really helped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment