Getting javascript to talk to lectora variables

Hi all - new to lectora and the software manual has very little on javascript use. Basically we have a test that X number of questions must be evaluated separately as they must be passed whether or not you get 100% in the others.


So we're using javascript to evaluate the answers given for the X number of Must Be Correct questions and working out the percentage. But no matter how we try, we cannot get the javascript to talk to the variable we have created in Lectora to receive the percentage (which will be used later to post to a result screen).


We've seen lots of "this is how to do it" posts but none actually work. Can someone post a really simple set of instructions on how to communicate with lectora variables from javascript, either in or out of a function?


many thanks


frank

Discussion (9)

So you have created a custom variable called "Greeting". This is the script to get itsvalue:

var gr = VarGreeting.getValue();


..and to set variable:

VarGreeting.set("hello");


That's it. It works. If it isn't working for you, you are doing something wrong. Post a sample if you want someone to look at it.

@elfanko 61840 wrote:

c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?


I assume "Sec_Question_1" is the variable name for the question? If so, it will contain the chosen answer text to that question. E.g. if the question is "What color is the sky?" with option "Red" and "Blue" and quiz point value of 10 points, the variable will contain "Red" or "Blue", not "10".


It's quite easy to see and understand all your errors if you look into the browser's console and check out the errors (like dividing a string by a number). Or use console.log JS command to output and check variable values. Or use alert() to the same effect. It will save you a lot of time.

Frank, Lectora variables are strings. Even if it contains a number, it's still a string. Dividing a string by a number results in error. Adding two strings like "4" + "12" produces "412", not 16.


So instead of this:

var Res = VarOne.getValue(); // it is "3", i.e. string


do this:

var Res = parseInt(VarOne.getValue()); // it is 3, i.e. number


Then you can manipulate your Res as number (divide, multiple, add).

@ssneg 61829 wrote:

So you have created a custom variable called "Greeting". This is the script to get itsvalue:

var gr = VarGreeting.getValue();


..and to set variable:

VarGreeting.set("hello");


That's it. It works. If it isn't working for you, you are doing something wrong. Post a sample if you want someone to look at it.



Hi there - thanks for the reply; ok we got 50% of this working.


We managed to set Lectora internal variables using .set and even managed a percentage calculation in the brackets to push an evaluated number into the variable.


Getting the variables from Lectora is proving more tricky.


Using the following script causes Lectora not to populate the VarSecPercentage variable:


var secTotalScore = VarSec_Question_1.getValue();

VarSecPercentage.set(secTotalScore/3*100);

[/CODE]

If we try just

[CODE]

VarSecPercentage.set(3/6*100);

[/CODE]

for example hard coding the numbers in, we get 50.....voila working.

If we try

[CODE]

var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();

VarSecPercentage.set(secTotalScore/3*100);

[/CODE]

again nothing.

So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.

To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.

Thanks for any help

Frank[CODE]

var secTotalScore = VarSec_Question_1.getValue();

VarSecPercentage.set(secTotalScore/3*100);

[/CODE]


If we try just


VarSecPercentage.set(3/6*100);

[/CODE]

for example hard coding the numbers in, we get 50.....voila working.

If we try

[CODE]

var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();

VarSecPercentage.set(secTotalScore/3*100);

[/CODE]

again nothing.

So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.

To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.

Thanks for any help

Frank[CODE]

VarSecPercentage.set(3/6*100);

[/CODE]


for example hard coding the numbers in, we get 50.....voila working.


If we try

var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();

VarSecPercentage.set(secTotalScore/3*100);

[/CODE]

again nothing.

So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.

To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.

Thanks for any help

Frank[CODE]

var secTotalScore = VarSec_Question_1.getValue()+VarSec_Question_2.getValue()+VarSec_Question_3.getValue();

VarSecPercentage.set(secTotalScore/3*100);

[/CODE]


again nothing.


So Lectora doesn't like the first line, either just a single variable call or multiple and adding them up.


To me this should just be basic Javascript but Lectora is throwing added spanners in the works with the use of Var and .set and .getValue.



Thanks for any help


Frank

@ssneg 61838 wrote:

Frank, Lectora variables are strings. Even if it contains a number, it's still a string. Dividing a string by a number results in error. Adding two strings like "4" + "12" produces "412", not 16.


So instead of this:

var Res = VarOne.getValue(); // it is "3", i.e. string


do this:

var Res = parseInt(VarOne.getValue()); // it is 3, i.e. number


Then you can manipulate your Res as number (divide, multiple, add).



Hi there - ok; we have tried -


var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(SecTotalScore);

[/CODE]

This should simply grab the score, as a string, convert it to an interger and then set the variable VarSecPercentage as that integer (in this case 1). The text area is not changed.

If we do

[CODE]

//var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(50);

[/CODE]

50 appears in the text box. So we can set the variable ok. And we can apply maths within .set() ok, so that all works. So it must be either a) we're not grabbing the variable from Lectora correctly or b) an error is occuring during parseInt.

Thanks for the suggestions - including Bens; the reason we're using javascript is that this test has say 100 questions. Within the 100 questions there are 10 you must get 100% right. So this is a separate javascript test to ensure those 10 are score 100% before the rest of the test is processed internally in Lectora.....convolted yes, but clients are clients :D

So breaking it down -

a) I assume I'm creating a javascript temp variable correctly - var SecTotalScore?

b) =parseInt(); conducts an interger change on any strings and passes it into SecTotalScore?

c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?

d) .set() accepts Javascript variables as parameters?

If yes to all of these, then what is going wrong if

VarSecPercentage.set(50); passes 50,

VarSecPercentage.set(3/6*100); passes 50,

but

var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(SecTotalScore);

does nothing

We're fairly new to Lectora and although not new to javascript, Lectoras implimentation of it is throwing a few spanners in the works! :)

cheers

frank[CODE]

var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(SecTotalScore);

[/CODE]


This should simply grab the score, as a string, convert it to an interger and then set the variable VarSecPercentage as that integer (in this case 1). The text area is not changed.


If we do


//var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(50);

[/CODE]

50 appears in the text box. So we can set the variable ok. And we can apply maths within .set() ok, so that all works. So it must be either a) we're not grabbing the variable from Lectora correctly or b) an error is occuring during parseInt.

Thanks for the suggestions - including Bens; the reason we're using javascript is that this test has say 100 questions. Within the 100 questions there are 10 you must get 100% right. So this is a separate javascript test to ensure those 10 are score 100% before the rest of the test is processed internally in Lectora.....convolted yes, but clients are clients :D

So breaking it down -

a) I assume I'm creating a javascript temp variable correctly - var SecTotalScore?

b) =parseInt(); conducts an interger change on any strings and passes it into SecTotalScore?

c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?

d) .set() accepts Javascript variables as parameters?

If yes to all of these, then what is going wrong if

VarSecPercentage.set(50); passes 50,

VarSecPercentage.set(3/6*100); passes 50,

but

var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(SecTotalScore);

does nothing

We're fairly new to Lectora and although not new to javascript, Lectoras implimentation of it is throwing a few spanners in the works! :)

cheers

frank[CODE]

//var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(50);

[/CODE]


50 appears in the text box. So we can set the variable ok. And we can apply maths within .set() ok, so that all works. So it must be either a) we're not grabbing the variable from Lectora correctly or b) an error is occuring during parseInt.


Thanks for the suggestions - including Bens; the reason we're using javascript is that this test has say 100 questions. Within the 100 questions there are 10 you must get 100% right. So this is a separate javascript test to ensure those 10 are score 100% before the rest of the test is processed internally in Lectora.....convolted yes, but clients are clients :D


So breaking it down -

a) I assume I'm creating a javascript temp variable correctly - var SecTotalScore?

b) =parseInt(); conducts an interger change on any strings and passes it into SecTotalScore?

c) VarSec_Question_1.getValue() gets me a string of the score given for the answer to Sec_Question_1?

d) .set() accepts Javascript variables as parameters?


If yes to all of these, then what is going wrong if

VarSecPercentage.set(50); passes 50,

VarSecPercentage.set(3/6*100); passes 50,

but

var SecTotalScore = parseInt(VarSec_Question_1.getValue());

VarSecPercentage.set(SecTotalScore);


does nothing


We're fairly new to Lectora and although not new to javascript, Lectoras implimentation of it is throwing a few spanners in the works! :)


cheers


frank

You can do all this in Lectora w/o JavaScript. If they are questions, the question varible has the answer choice selected, not the value or weight of the question in the overall score. Generally unless you change it, questions are weighted 1 pt. So you have to test the question for correctness and add 1 to a total if it is correct.



Ihn Lectora it would look something like this:


Modify Variable _must_Score Set 0

Modify Variable _must_Score Add 1; Condition: Question_0001 Is Correct

Modify Variable _must_Score Add 1; Condition: Question_0002 Is Correct

Modify Variable _must_Score Add 1; Condition: Question_0003 Is Correct

Modify Variable _must_Score Divide 3

Modify Variable _must_Score Multiply 100


Now it is maintainable by a Lectora developer who does not know JavaScript.

@benpitman 61839 wrote:

You can do all this in Lectora w/o JavaScript. If they are questions, the question varible has the answer choice selected, not the value or weight of the question in the overall score. Generally unless you change it, questions are weighted 1 pt. So you have to test the question for correctness and add 1 to a total if it is correct.



Ihn Lectora it would look something like this:


Modify Variable _must_Score Set 0

Modify Variable _must_Score Add 1; Condition: Question_0001 Is Correct

Modify Variable _must_Score Add 1; Condition: Question_0002 Is Correct

Modify Variable _must_Score Add 1; Condition: Question_0003 Is Correct

Modify Variable _must_Score Divide 3

Modify Variable _must_Score Multiply 100


Now it is maintainable by a Lectora developer who does not know JavaScript.


Yep, as much as I like JavaScript, this is exactly what I'd do in this case.

Hi guys - thanks for your help on this; this is now resolved.



Yes we found just before checking this thread that the variable for the question contains the answer given not the score....d'oh - lesson learned. :)


So then we went through Bens solution and it worked perfectly. I was trying to solve the equation through a programmers eyes with my content creator colleague. Bens solution solves the problem completely, I just couldn't see it being a programmer :)


kind regards to all involved


frank

Secrets revealed -- I was a programmer for some 20 years before I got into training. The secret was to know what the value of the variable was. In future, use Debug option in either run mode or HTML and watch the value of the variables.


Glad it worked for you

Discussions have been disabled for this post