Scan short answer for keywords

Hello Everyone,

I am currently working on an assessment for a project. One of the questions require the learners to type in their response, however, the response needs to contain three keywords to make it correct.

I know this can be done using javascript but i do not know how.

What I have done:

1. I created two variables on storyline "quizanswer" and "res"

2. wrote this script in javascript

var player = GetPlayer();
var quizanswer = player.GetVar("quizanswer");
var resp = answer.match(/interacting|interdependent|unified whole/gi);
player.SetVar ("res", resp);

3. added a trigger to show correct if res != null and show incorrect if res = null

but it is not working.

Please help! :'( 

5 Replies
Christie Pollick

Hi, Ademola -- Thanks for reaching out with your question! I will need to defer to the community to chime in with their advice on using JavaScript (as that is something for which we do not provide support), however, I wanted to be sure to provide our JS Best Practices sheet in case you'd like to keep it handy. Best of luck with your project. :)

Matthew Bibby

It looks like your JavaScript variable names don't match. 

Rather than:

var player = GetPlayer();
var quizanswer = player.GetVar("quizanswer");
var resp = answer.match(/interacting|interdependent|unified whole/gi);
player.SetVar ("res", resp);

Try this:

var player = GetPlayer();
var answer = player.GetVar("quizanswer");
var resp = answer.match(/interacting|interdependent|unified whole/gi);
player.SetVar ("res", resp);
Ademola Johnson

Thank you Matthew,

I noticed that and corrected it and somehow managed to get it to work but the logic in between the keywords is 'or' which would pass if either of the keywords are available. I wanted it to be 'and' i.e all keywords must be present.

Secondly if the question is passed, it does not change the overall test score. I don't know why. Can you please help?

Matthew Bibby

Hi Ademola,

I'm not sure. I know very little JavaScript, although I am learning more every day. Unfortunately, I wasn't able to solve this one. I'm sure someone who knows JavaScript would be able to figure this out quickly.

In the meantime, you could always do something like this:

var player = GetPlayer();
var answer = player.GetVar("quizanswer");
var resp1 = answer.match(/interacting/gi);
var resp2 = answer.match(/interdependent/gi);
var resp3 = answer.match(/unified whole/gi);
player.SetVar ("res1", resp1);
player.SetVar ("res2", resp2);
player.SetVar ("res3", resp3);

And then use Storyline triggers to adjust a variable if res1 = interacting, res2 = interdependent and res3 = unified whole. It's hardly glamorous, but it's the best solution I can come up with in the little time I have available. 

If someone else can answer this one I'd be interested to see the JavaScript solution.