Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Fiddle print/alert

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));
like image 664
mjmitche Avatar asked Apr 04 '11 00:04

mjmitche


1 Answers

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

like image 194
Khez Avatar answered Sep 20 '22 04:09

Khez