JSFiddle - React, Tailwind, and code Playground
HTML
<script>
var _$parentEntry = null;
$(document).ready(function() {
_$parentEntry = $("#Console");
});
function Debout() {
var $logEntry = null;
var $parentEntry = _$parentEntry;
var logEntry = function() {
if ($logEntry) return $logEntry;
$logEntry = $("<div class='logEntry'></div>");
$parentEntry.append($logEntry);
_$parentEntry = $logEntry;
return $logEntry;
}
this.print = function(text) {
logEntry().append("<span>" + text + "</span>");
}
this.close = function() {
_$parentEntry = $parentEntry;
}
}
function findSequence(goal) {
function find(start, history) {
var res = null;
var debout = new Debout();
debout.print("<b>find(" + start + ", \"" + history + "\")</b>");
if (start == goal)
{
debout.print(" returns \"" + history + "\"");
res = history;
}
else if (start > goal)
{
debout.print(" returns null");
res = null;
}
else
{
var oneResult = null;
var otherResult = null;
debout.print(" -- trying with " + start + " + 5...");
oneResult = find(start + 5, "(" + history + " + 5)");
if (oneResult == null)
{
debout.print("...no good. Trying with " + start + " * 3...");
otherResult = find(start * 3, "(" + history + " * 3)");
if (otherResult == null)
{
debout.print("...also no good.");
}
else
{
debout.print("...fine.");
}
}
else
{
debout.print("...fine.");
}
debout.print(" returns " + (oneResult || otherResult || "null"));
res = oneResult || otherResult;
}
debout.close();
return res;
}
return find(1, "1");
}
</script>
Goal: <input id="Goal" type="text" value="24" />
<button onclick="$('#Result').text(findSequence(parseInt($('#Goal').val())))">Find sequence</button>
<div...
CSS
#Result
{
background-color: black;
padding: 1em;
font-size: 1.4em;
font-weight: bold;
color: white;
}
.logEntry
{
--margin-left: 0.5em;
padding-left: 1em;
border-left: 1px solid #c0c0c0;
}