DevByPowerPoint

ELHC 471 Game Design and Prototyping

Jul 26th, 2024 (edited)
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is the main function that compares the learner's answer to the correct answer
  2. function compareAnswers() {
  3.     // Get access to Storyline's features
  4.     var player = GetPlayer();
  5.    
  6.     // Get the learner's answer and the correct answer from Storyline
  7.     var learnerAnswer = player.GetVar("Learner_Answer");
  8.     var correctAnswer = player.GetVar("Correct_Answer");
  9.    
  10.     // Clean up both answers (remove extra spaces, make uppercase, ignore punctuation, etc.)
  11.     var processedLearnerAnswer = processAnswer(learnerAnswer);
  12.     var processedCorrectAnswer = processAnswer(correctAnswer);
  13.    
  14.     // Calculate how accurate the learner's answer is (0-100%)
  15.     var score = calculateScore(processedLearnerAnswer, processedCorrectAnswer);
  16.    
  17.     // Check if the learner's answer is exactly correct or contains the entire correct answer
  18.     var containsCorrectAnswer = (score === 100) || checkContainsCorrectAnswer(processedLearnerAnswer, processedCorrectAnswer);
  19.    
  20.     // Update the accuracy score in Storyline
  21.     player.SetVar("Accuracy_Score", score);
  22.    
  23.     // Update whether the learner's answer is correct or contains the correct answer in Storyline
  24.     player.SetVar("Contains_Correct_Answer", containsCorrectAnswer);
  25.  
  26.     // If the answer is completely wrong, briefly change the score to 1% and back to 0%
  27.     // This helps trigger Storyline events even when the score doesn't change from 0%
  28.     if (score === 0 && !containsCorrectAnswer) {
  29.         brieflyChangeScore(player);
  30.     }
  31. }
  32.  
  33. // This function cleans up the answers for easier comparison
  34. function processAnswer(answer) {
  35.     // Remove all punctuation (including within words), convert to uppercase, and remove extra spaces
  36.     return answer.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "").trim().toUpperCase().replace(/\s+/g, " ");
  37. }
  38.  
  39. // This function calculates how accurate the learner's answer is
  40. function calculateScore(learnerAnswer, correctAnswer) {
  41.     if (learnerAnswer === correctAnswer) {
  42.         return 100; // If the answers match exactly, give 100%
  43.     } else if (learnerAnswer.includes(correctAnswer)) {
  44.         return 90; // If learner's answer contains the correct answer but has extra content, give 90%
  45.     } else {
  46.         // For partially correct answers, calculate a score based on how many correct words are present
  47.         var correctWords = correctAnswer.split(" ");
  48.         var matchedWords = correctWords.filter(word => learnerAnswer.includes(word));
  49.         return Math.round((matchedWords.length / correctWords.length) * 80); // Max 80% for partial matches
  50.     }
  51. }
  52.  
  53. // This function checks if the learner's answer contains the entire correct answer
  54. function checkContainsCorrectAnswer(learnerAnswer, correctAnswer) {
  55.     return learnerAnswer.includes(correctAnswer);
  56. }
  57.  
  58. // This function briefly changes the score from 0% to 1% and back
  59. // It helps trigger Storyline events when the answer is completely wrong
  60. function brieflyChangeScore(player) {
  61.     player.SetVar("Accuracy_Score", 1); // Set score to 1%
  62.     setTimeout(function() {
  63.         player.SetVar("Accuracy_Score", 0); // Set score back to 0% after a very short delay
  64.     }, 50); // 50 milliseconds = 0.05 seconds
  65. }
  66.  
  67. // This line runs the main function when the script is executed
  68. compareAnswers();
Advertisement
Add Comment
Please, Sign In to add comment