Update. No problem with this question. See first comment.
I`m trying to figure out this code with jsfiddle.net, but when I run it, it triggers the actual printer attached to my computer. I changed print to "alert" http://jsfiddle.net/eZ3jQ/ and it returned (((1 * 3) + 5) * 3). However, as the return calls find, I expected it to run find over again.
Is there a way I can get the program to keep running?
function findSequence(goal) {
function find(start, history) {
if (start == goal)
return history;
else if (start > goal)
return null;
else
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
}
return find(1, "1");
}
print(findSequence(24));
Well, apart from the fix that wasn't needed, lemme make a suggestion.
HTML
<div id="logs"></div>
JS
var logs=document.getElementById('logs');
function logIt(msg){
var e=document.createElement('div');
e.innerHTML=msg;
logs.insertBefore(e,logs.firstChild);
}
Log function that prepends messages, useful so you can keep track of things. Alert is nasty :P
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With