7

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));
mjmitche
  • 2,077
  • 5
  • 24
  • 31

2 Answers2

10

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

Khez
  • 10,172
  • 2
  • 31
  • 51
  • 6
    Or, if you are using a browser which can handle it (Firefox+Firebug, Chrome, Safari, etc.) use `console.log()` and it's siblings. Very nice for discrete logging and error recording, especially in a development environment. – Luke Stevenson Apr 04 '11 at 01:40
  • I find firebug to be a must for development, I have met people that disregard it entirely. In the end, all web development is 80% IE issues and that's where console.log isn't useful :D – Khez Apr 04 '11 at 01:42
  • 1
    I think that IE8 now has a port of Firebug called "Developer Tools". Plus there is Firebug Lite, which runs in nearly anything. Meaning `console.log()` is slowly becoming universal, more or less. – Luke Stevenson Apr 06 '11 at 07:11
  • If you want the log to append messages, change `logs.insertBefore(e, logs.firstChild)` to `logs.appendChild(e)`. – blackcatweb Feb 25 '13 at 19:15
0

HTML:

<div id="log"></div>

JS:

var log = function(msg) {
    var elem = document.getElementById('log');
    elem.innerHTML += '<p>' + msg + '</p>';
};
neurosnap
  • 5,658
  • 4
  • 26
  • 30