Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is the main function that compares the learner's answer to the correct answer
- function compareAnswers() {
- // Get access to Storyline's features
- var player = GetPlayer();
- // Get the learner's answer and the correct answer from Storyline
- var learnerAnswer = player.GetVar("Learner_Answer");
- var correctAnswer = player.GetVar("Correct_Answer");
- // Clean up both answers (remove extra spaces, make uppercase, ignore punctuation, etc.)
- var processedLearnerAnswer = processAnswer(learnerAnswer);
- var processedCorrectAnswer = processAnswer(correctAnswer);
- // Calculate how accurate the learner's answer is (0-100%)
- var score = calculateScore(processedLearnerAnswer, processedCorrectAnswer);
- // Check if the learner's answer is exactly correct or contains the entire correct answer
- var containsCorrectAnswer = (score === 100) || checkContainsCorrectAnswer(processedLearnerAnswer, processedCorrectAnswer);
- // Update the accuracy score in Storyline
- player.SetVar("Accuracy_Score", score);
- // Update whether the learner's answer is correct or contains the correct answer in Storyline
- player.SetVar("Contains_Correct_Answer", containsCorrectAnswer);
- // If the answer is completely wrong, briefly change the score to 1% and back to 0%
- // This helps trigger Storyline events even when the score doesn't change from 0%
- if (score === 0 && !containsCorrectAnswer) {
- brieflyChangeScore(player);
- }
- }
- // This function cleans up the answers for easier comparison
- function processAnswer(answer) {
- // Remove all punctuation (including within words), convert to uppercase, and remove extra spaces
- return answer.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "").trim().toUpperCase().replace(/\s+/g, " ");
- }
- // This function calculates how accurate the learner's answer is
- function calculateScore(learnerAnswer, correctAnswer) {
- if (learnerAnswer === correctAnswer) {
- return 100; // If the answers match exactly, give 100%
- } else if (learnerAnswer.includes(correctAnswer)) {
- return 90; // If learner's answer contains the correct answer but has extra content, give 90%
- } else {
- // For partially correct answers, calculate a score based on how many correct words are present
- var correctWords = correctAnswer.split(" ");
- var matchedWords = correctWords.filter(word => learnerAnswer.includes(word));
- return Math.round((matchedWords.length / correctWords.length) * 80); // Max 80% for partial matches
- }
- }
- // This function checks if the learner's answer contains the entire correct answer
- function checkContainsCorrectAnswer(learnerAnswer, correctAnswer) {
- return learnerAnswer.includes(correctAnswer);
- }
- // This function briefly changes the score from 0% to 1% and back
- // It helps trigger Storyline events when the answer is completely wrong
- function brieflyChangeScore(player) {
- player.SetVar("Accuracy_Score", 1); // Set score to 1%
- setTimeout(function() {
- player.SetVar("Accuracy_Score", 0); // Set score back to 0% after a very short delay
- }, 50); // 50 milliseconds = 0.05 seconds
- }
- // This line runs the main function when the script is executed
- compareAnswers();
Advertisement
Add Comment
Please, Sign In to add comment