JavaScript Decision Tree tool

My goal has been to make a JavaScript Decision Tree tool as a new content module for our next site redesign.

It needs to work such that the same piece of JavaScript can be utilised without modification for any decision tree tool we build on the site, with a very simply-defined markup structure,

So I’ve been self-administering repeated doses of headdesk/facepalm over the last couple of days, but I came back today after a Friday lunch (you know what I mean) and found a solution. It’s not pretty, with at least two nested ‘for’ loops, but it works.

I’ve created a JSFiddle of it, so you can take a look at it, modify it, laugh at it, etc.
Open it in JSFiddle or see it embedded below:

This entry was posted in Coding, JavaScript. Bookmark the permalink.

2 Responses to JavaScript Decision Tree tool

  1. JP says:

    js fiddle . . . sounds dodgy . . .

  2. Steve says:

    Reading through your code has been very worthwhile to further by JS knowledge. I have to take a different approach as I am using a tag buttons instead of radios so need a way of persisting what was chosen before.

    I can suggest adding comments in for each for loop as it is quite hard to grasp at first what is going on.

    You can also move the num_of_steps calculation up a level as it only needs to be calculated once and not on every click.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>