I am developing a project for higher education where hexadecimal / Binary numbers are involved in final assessment. I am looking for an input field where User should enter a range of hexadecimal value (from 20000000 to 200FFFFF). And I should be able to validate its correctness.
yes, using text entry field, user can insert both letters and numbers. My challenge is with validating the answer. We have almost 4000 possible correct answers (hexadecimal value from 20000000 to 200FFFFF) and i cant have them all 4000 entered manually (for validation). Is there a way to give this hexadecimal range for validation?
That's a lot of possible correct answers! It may be something you could accomplish using Javascript, but I'll have to rely on our community members to offer some more help on that!
I realized my answer was only a partial answer in that my code only validated whether or not the number was a valid hexadecimal number. Once you validate this, you would still need to check if it is within the range. For me, the easy way to do this would be to convert your range values and the user input to integers and then evaluate the values.
Updated (and commented) code including the calls to StoryLine below (Note that I am using the following text variables in StoryLine: HexValue, IsValid, & IsBetween.
//Find the SL Player and store the user's input in a JS variable var player = GetPlayer(); var testString = player.GetVar("HexValue");
//Create a function to test the user's input to determine if it is a valid Hex number function is_hexadecimal(testString) { regexp = /^[0-9a-fA-F]+$/; if (regexp.test(testString)) { return true; } else { return false; } }
//Run the function and send a true or false value back to SL var myTestHex = is_hexadecimal(testString); player.SetVar("IsValid", myTestHex)
//Convert valid hex to integer and evaluate if user input is within the specified value range if (myTestHex == true) { var x = parseInt("20000000", 16); var y = parseInt(testString, 16); var z = parseInt("200FFFFF", 16); if (x<y && y<z) { var validResult = "true"; player.SetVar ("IsBetween", validResult); } else { var validResult = "false"; player.SetVar ("IsBetween", validResult); } } else { var validResult = "n/a"; player.SetVar ("IsBetween", validResult); }
I was thinking of using a graded numeric quizzing slide and adding a text input field to the slide.
For the acceptable numeric values set it to: Value is Between 536870912 and 537919487
Those are the decimal values of 20000000 and 200FFFFF
Then add a execute javascript trigger that converts the hexadecimal input value to a decimal value. Make sure the javascript trigger is before the submit interaction trigger.
var player = GetPlayer();
var q1Input = player.GetVar("q1Input"); var nDecimal = parseInt(q1Input, 16);
Without first validating that the response is an allowed Hexadecimal, you run the risk of giving false positives. For example, try inputting 2000000fz78 and it will tell you it is correct, even though this is not a valid Hex number.
10 Replies
Hi Imgk,
Have you looked at using a standard text entry field? That would allow you to insert letters and numbers.
If you need to validate what a user entered against a set of correct answers, look at using the Text entry question types such as a Fill in the Blank.
Let us know if you need anything else!
Hi Ashley, Thanks for your response.
yes, using text entry field, user can insert both letters and numbers. My challenge is with validating the answer. We have almost 4000 possible correct answers (hexadecimal value from 20000000 to 200FFFFF) and i cant have them all 4000 entered manually (for validation). Is there a way to give this hexadecimal range for validation?
Thanks again for your help.
That's a lot of possible correct answers! It may be something you could accomplish using Javascript, but I'll have to rely on our community members to offer some more help on that!
It would look something like....
var testString = "your storyline variable";
function is_hexadecimal(testString) {
regexp = /^[0-9a-fA-F]+$/;
if (regexp.test(testString)) {
return true;
} else
{
return false;
}
}
var myTestHex = is_hexadecimal(testString);
That last variable will store either true or false and you can send that value back to SL.
Wow! thanks all for the responses... Let me go through this and get back.
Owen and Matthew to the rescue! Thanks guys. 😀
I realized my answer was only a partial answer in that my code only validated whether or not the number was a valid hexadecimal number. Once you validate this, you would still need to check if it is within the range. For me, the easy way to do this would be to convert your range values and the user input to integers and then evaluate the values.
Updated (and commented) code including the calls to StoryLine below (Note that I am using the following text variables in StoryLine: HexValue, IsValid, & IsBetween.
//Find the SL Player and store the user's input in a JS variable
var player = GetPlayer();
var testString = player.GetVar("HexValue");
//Create a function to test the user's input to determine if it is a valid Hex number
function is_hexadecimal(testString) {
regexp = /^[0-9a-fA-F]+$/;
if (regexp.test(testString)) {
return true;
} else {
return false;
}
}
//Run the function and send a true or false value back to SL
var myTestHex = is_hexadecimal(testString);
player.SetVar("IsValid", myTestHex)
//Convert valid hex to integer and evaluate if user input is within the specified value range
if (myTestHex == true) {
var x = parseInt("20000000", 16);
var y = parseInt(testString, 16);
var z = parseInt("200FFFFF", 16);
if (x<y && y<z) {
var validResult = "true";
player.SetVar ("IsBetween", validResult);
} else {
var validResult = "false";
player.SetVar ("IsBetween", validResult);
}
} else {
var validResult = "n/a";
player.SetVar ("IsBetween", validResult);
}
See it work (sl360) here.
SL2 file attached below.
I was thinking of using a graded numeric quizzing slide and adding a text input field to the slide.
For the acceptable numeric values set it to:
Value is Between 536870912 and 537919487
Those are the decimal values of 20000000 and 200FFFFF
Then add a execute javascript trigger that converts the hexadecimal input value to a decimal value. Make sure the javascript trigger is before the submit interaction trigger.
var player = GetPlayer();
var q1Input = player.GetVar("q1Input");
var nDecimal = parseInt(q1Input, 16);
player.SetVar("q1Answer",nDecimal);
Without first validating that the response is an allowed Hexadecimal, you run the risk of giving false positives. For example, try inputting 2000000fz78 and it will tell you it is correct, even though this is not a valid Hex number.
Thanks Owen,
I didn't know that would happen.
I have attached an updated project that includes your is_hexadecimal function.
This discussion is closed. You can start a new discussion or contact Articulate Support.