<div>
<label>Enter expression with operator replacement "_":</label><br>
<input type="text" id="exp">
</div>
<div>
<label>Enter answer:</label><br>
<input type="text" id="ans">
</div>
<div>
<label>Enter operators (do not separate by any character):</label><br>
<input type="text" id="o">
</div>
<button id="btn">Run</button>
<p>Run at own risk of your browser lagging. Besure the operators are evaluatable using JavaScript.</p>
<div id="output"></div>
JavaScript
$(function() {
$("#btn").click(function() {
$("#output").html("");
var operators = $("#o").val().split(""),
exp = $("#exp").val(),
slots = 0;
while (exp.match(/_/))
exp = exp.replace(/_/, "${o[" + slots++ + "]}");
var max = Math.pow(operators.length, slots);
for (var i = 0; i < max; i++) {
var b = i.toString(Math.max(operators.length, 2)).pad(slots).split(""),
o = [];
b.forEach(item => o.push(operators[item]));
var t = eval("`" + exp + "`");
if (eval(t) == $("#ans").val())
$("#output").append(t + "<br>");
}
});
});
String.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.