my python code - for javascript? - javascript

I was wondering if someone could give me advice on this code. I have done it in python but I think I need to have it in javascript as it is for a website. I am new to programming so please be kind!
Aims of the site:
The user will have to answer 6 multiple choice questions. (Q1 has
7 possible answers but the others only have 2).
Depending on their inputs they will receive an outcome (I have just put the outcomes as range(1,225) for now but there will be different outcomes depending on the input
The outcomes and the possible input combinations are all fixed and will not change
I am pretty sure I have not done it the best way as I don't have much experience, but it seems to work so far.
Does the code look ok?
Do you think I will be able to translate this into javascript easily enough?
Should I have the table of outcomes/inputs fixed in some way so it doesn't need to be worked out by the computer every time or is it ok as it is?
Any advice or help is very much appreciated indeed.
#list of possible inputs
list = [[23,24,25,26,27,28,29],["male","female"],["true","false"],["true","false"],
["true","false"],["true","false"]]
#make a list of outcomes
outcome=[]
for i in range(1,225):
outcome.append(i)
#make a table of all possible list input combinations
r=[[]]
for e in list:
table = []
for item in e:
for i in r:
table.append(i+[item])
r = table
#make a dictionary where the input is the key and outcome is the value
adict = dict((str(r), outcome) for r, outcome in zip(r, outcome))
#dummy inputs as an example
input1 = 27
input2 = "male"
input3 = "true"
input4="true"
input5="true"
input6="true"
#put all the inputs into one string & look up outcome in adict
new_input = []
new_input.extend([input1,input2,input3,input4,input5,input6])
print adict.get(str(new_input))

There is no need to rewrite it in javascript; instead try using one of the Python web frameworks like Flask or Django.

Related

Semi(Auto) Code Generating with Python/Java/etc

Before anyone down votes or call out as duplicate. I searched for code generation, auto code complete, code complete with python, and code automation with Java/Python and every results I have got were not relevant to task I want to achieve.
I am free to use any language but Java or Python are preferred due to various libraries and api support.
Task I have is: need to write a program that outputs *.js file and in that file I am going to have a same function printed many time as number of inputs.
Here Only thing that changes is name of the function and to speak.
n These two inputs are passed in as CSV file like column one is NAME and Column two is MESSAGE(to speak)
---------------------------------- output.js -------------------------------
module.exports = {
john: john,
tony: tony,
laura: laura
};
function john(assistant) {
let toSpeak = "Here something goes for John";
assistant.setContext("navigator", 1, {"mynavigator": OPTIONS});
return askAssistant(toSpeak, assistant);
function tony(assistant) {
let toSpeak = "whatever is message for tony";
assistant.setContext("navigator", 1, {"mynavigator": OPTIONS});
return askAssistant(toSpeak, assistant);
function laura(assistant) {
let toSpeak = "I think you got where I am going with";
assistant.setContext("navigator", 1, {"mynavigator": OPTIONS});
return askAssistant(toSpeak, assistant)
-----------------------------output.js------------------------------------------
I am not asking anyone to code this for me rather suggest me a tool that would help me achieve this task. I will sincerely appreciate your kind feedback
I can think of two approaches you could take.
The first approach is to use print statements in your favourite programming language to print out the required text. And, of course, wrap some of those print statements in for-loops to iterate over the list of (name, message) tuples. This approach is straightforward and does not require use of any third-party code-generation tools.
The second approach is to use a template engine, such as Apache Velocity (for Java).

How to get value of Qualtrics Drag & Drop Choice using Javascript

I have what should be a simple answer. I have a drag and drop style multiple choice question on Qualtrics. I have recoded the answers as I wish. I think it assigns to each answer a variable like QID15_1 QID15_2 QID15_3, etc. The outputted data then gives the rank order of that selected option.
So if I ranked them, for example
QID15_3
QID15_1
QID15_2
The value of QID15_3 =1, the value of QID15_1=2, and the value of QID15_2=3.
What is the correct syntax to access these values? I want to set an Embedded Data item with the value of QID15_1, for example (so it should equal 2), but I can't seem to get it correct.
I've tried Qualtrics.SurveyEngine.getSelectedAnswerValue and things like this.
Any help would be appreciated. Thanks!
EDIT: With the help of a friend who knows Java, we figured it out. You can write something like
Qualtrics.SurveyEngine.addOnUnload(function()
{var order=document.getElementById( 'QR~QID15~1' ).getElementsByClassName( 'rank' )[0].innerText;
}
You don't need to set an embedded variable in JavaScript. You can just pipe the value wherever you need it (in a subsequent question, in the survey flow, etc.). It would be:
${q://QIDx/ChoiceNumericEntryValue/y}
where x is the question id and y is the choice id.
EDIT:
You can assign it to an embedded data variable in the survey flow like this:
edvar = ${q://QIDx/ChoiceNumericEntryValue/y}

Is there an easy solution to find a result depending on 3 data?

I would like to know if there is an "easy" solution in Javascript to find a result depending on 3 differents paremeters without using a lot of "if" or "switch".
My case: I have 3 dropdown lists, each of them allow the user to chose a number between 0 and 5.
Having the 3 results, I would like to pick the corresponding result (in my case, redirecting to a specific URL).
Ex: The user chooses 2 0 4 -> specific url.
This is not really a "javascript" question but more a program logic question but keep in mind I need to do it in javascript.
I was thinking about a 3 dimensions table but can't figure out how to initiate and use it..
Thanks in advance for any help.
I would do something like the following:
Say you have a button to go to the url: call it #my-button:
Have a click event that does something like:
$('#my-button').click(function(){
base_url = 'http:www.xyz.com?sfdasf=' + $('#selctor1').val() + $('#selctor2').val() + $('#selctor3').val();
//Now execute
})
The idea is to build the url string using what exists in the selectors. This of course depends on if the urls use the information in the selector dropdowns.
Hope this helps!

Enter Numbers Into HTML Form

I have 10 small text field boxes in HTML where I want users to enter numbers 1 to 10 - and the same number can not be entered twice in another box.
I'm struggling to work out how to accomplish this in Javascript/jQuery.
Any help would be appreciated.
Simply create an array with the values and check for duplication.
var formdata=new Array();
formdata[0]=document.getElementByid('id').value;
formdata[1]=document.getElementByid('id').value;
formdata[2]=document.getElementByid('id').value;
Now check for duplications in the array using the following - I guess the following question would be useful on stack overflow:
Easiest way to find duplicate values in a JavaScript array.
If the breakpoint is one simply display an alert() to the user to renter the data.
Instead of using documents.getElementById() you can also use
oForm = document.forms[index];
oText = oForm.elements["text_element_name"].value; OR
oText = oForm.elements[index].value;
More information can be found at:
http://www.javascript-coder.com/javascript-form/javascript-get-form.phtml

Creating variable arrays with Javascript (dyamic, drop down)

I've been searching the board for a while (both here and Google) and can't seem to find what I'm looking. (Sorry if I've missed it and this qualifies as an annoying/redundant question.)
I'm working on form that will have copious amounts of drop downs based on previous selected variable(s). I was trying to come up with an easier way than having to create the individual fields and then hide/show based on selection. Ultimately, I'm aiming for a "tree" with between 3-5 levels of menus. (Clients doing, not mine.)
What the Logic looks like:
Variables: type, offer1, offer2, insert1, insert2, insert3,...
Where [type] determines [offer1,2] which has up to 3 variables each [insert1,2,3...]
So If user selects Type A: Offer1 = Array A (Insert1 = ArrayA1, Insert2 = ArrayA2, Insert3 = null) and Offer2 = Array B (Insert4 = ArrayB4, Insert5 = null); and so on and so forth.
So far, everything I've found only seems to handle the first tier, and JS isn't exactly my forte. Any pointing in the right direction would be greatly appreciated.
In case anyone else runs across this issue, I found this solution that uses JQuery:
http://www.appelsiini.net/projects/chained

Categories

Resources