Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Regex + eval solution in Clear category for [old] Safe Code by SaintDron
"use strict";
function safeCode(equation) {
equation = equation.replace(/=/, '===').replace(/--/g, '+');
for (let n = 0; n <= 9; n++) {
if (equation.includes(n)) continue;
let eq = equation.replace(/#/g, n);
if (n === 0 && ~eq.search(/(^|\D)0\d+/)) continue;
if (eval(eq)) return n;
}
return -1;
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(safeCode("-5#*-1=5#"))
// These "asserts" are used for self-checking and not for an auto-testing
assert.equal(safeCode("-5#*-1=5#"), 0)
assert.equal(safeCode("##*##=302#"), 5)
assert.equal(safeCode("19--45=5#"), -1)
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Sept. 15, 2018
Comments: