Question:
Is javascript the wrong tool for this task?
?
2019-10-23 18:23:38 UTC
So i want to create a program where the user gets questions, one after the other, and based on his answers he will be directed to a tree of questions, and inside this tree of questions there could be more tree’s, based on his input.

And i’ve been learning javascript this past month’s trying to achieve this.
And achieved a nice progress with it in, but the code is getting very very complicated due to the nature of javascript being a loop based language, so i need to create so many rules and conditions so the program will know what question to load and which one to regard. 
And it’s like all the questions are being processed all at the same time, and i need to manipulate what question the user is seeing, although really everything is being rendered recursively where one question renders the next one after the user submits, so it will never leave the main question function.
Unlike a normal programming language, where i could simply call one function at a time, wait for the user to respond, and then call the next function with no issue, no counter or anything like that..
So i’m considering now that javascript may not be the best tool for the job.
Do you agree? can you recommend something else to invest in? i’m open for suggestions.
I thought this would be a simple Q & A program, but it turns out it’s much more difficult.
Thanks.
Five answers:
Robert J
2019-10-23 20:25:48 UTC
You should be able to create that type of system in any language that supports arrays or structures.



You only need a very simple loop program.



Store the questions in an array, and have another 2D array (or four separate 1D arrays of ints) saying which question to go to next for each answer.



Start at the first array index when the program starts.

Print that question, get an answer number, then get the new question number from the numeric array entries for that same index.



Set the new index as needed from that value and repeat.



Use special "answer" lookup numbers for different conditions other just going to a question, eg. high or negative numbers - eg. to end or go to a result screen etc.



(Or if you prefer structures, have something like string, int, int, int, int for the question and four answer numbers; an array of those of whatever size needed for all the questions).



In effect, you are building an interpreter for a "program" of question data and by loading a different set of data you can use exactly the same program for different things.
?
2019-10-25 08:24:39 UTC
Yeah, in-browser JS is event-based which can be confusing if you're used to sequential programming. However your approach is also completely wrong, so there's that :) The first step is to build an array of questions and have each answer contain the id of the next question to show (i.e. tree branch)



Next you write a program that loads each question starting with the first, then processes the clicked answer.
husoski
2019-10-23 20:24:00 UTC
JS is definitely good enough for this sort of task.  I'd probably choose something else for idiosyncratic reasons (I don't like the JS array model, and until recently there was no class-based OOP or namespaces), but that more about taste than functionality.



If you want to run like a "normal language", you have options.  Node.JS is a standalone JavaScript implementation based on the engine in the Chrome browser.  You don't have to run in a browser. 



On the weirder side, is Microsoft's JScript interpreter, present in every Windows installation since Windows 2000.  It has its own class library and is more oriented toward admin scripting.



I suspect that the choice of representing this Q&A as code rather than data is the thing that's causing you more pain than any JS quirks, though.  You might want to think about representing your Q&A as some form of a tree or graph. 



You can use JSON format to encode the data structure, easily getting the data into a form your program can use.  Not only that, but most other languages have JSON libraries either built-in or readily available from 3rd parties, so you can use other languages if you like to either implement the Q&A for a different environment than the browser or Node.JS in a console, or write offline tools to make editing the JSON file and/or checking for errors easier.
VP
2019-10-23 19:59:08 UTC
Do you know any other languages? If not, try learning some Python v3.xx and see if you get farther. Python appears to be one of the most flexible languages I've ever come across. Any easier and the damn thing will write the program for you! 🤣



Just search for 'Python free tutorials' and I'm sure you'll find one that fits.

Good luck!
?
2019-10-23 18:28:18 UTC
There are programs like that determining a persons general mental state. Either normal, sociopath, psychopathic etc. Sounds like it could get pretty complex. You could make it very simple at the same time though. For instance just giving a question two options, yes and no, rather than it being a more multiple choice deal. Yes and no makes sending a user down a particular route a bit easier. If they choose yes, they go into one category, if they choose no, they go into another. I think in that case you could only have two outcomes though. It can make having more than two outcomes more difficult if the tree itself is made less complex. If its a percentage on multiple outcomes the user is presented with at the end, you could make it very easy. Say you had 30 questions stranding down one after the other rather than forming an actual program that literally branches everything out in a more complex way based on an answer given, the user is just presented with the percentage mentality in the case of maybe 4 options, average, sociopaths, psychopathic, narcissistic etc. This just in the case of the example I brought up to help explain my points. Doing it that way allows a user to know what their mentality is closer to rather than just being told "mate you're a psychopath". One is less complex but in a sense gives the user a much better answer. The thing with programming is that you'll spend a long time trying to make it way more complex than it actually has to be.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...