Copy link to clipboard
Copied
Hi,
I am working for something and trying to create a type-box-based quiz for one of my classes, where an input text field can change it's border color if 2 or more words from an accepted word list is inputed.
For example, Say a list has possible answers: R,G, B, Y for the question "Name 2 colors".
I want to create an input text box where the border of the box will change color if any 2 letters (in the above list) are entered into it.
I've tried setting up a variable NumberSuccesses and setting it so that the number increases by 1 for each letter entered e.g.
if (Ex.text == "R");
NumSuc = NumSuc +1; // the var NumSuc has already been defined earlier in the code
If (Ex.text ''G")
NumSuc = NumSuc + 1
if (NumSuc == 2)
Ex.borderColor = 0x0000FF
but that didn't work.. and I tried doing it with another function which I use for multiple input boxes like if there were boxes A, B, C. I could set up a general function where if the correct answers were entered into A, B, an C respectively, only then will say, a checkmark show up using a
EnableCheckmark (); type function. Not sure how to do it if it's the same text input box though. Also just tried adding it into the same function re.
If (Ex.text == "R" + "G")
Ex.borderColor = 0x0000FF
.. Does anyone know what else I can do?
The solution of Nishu with Evaluation Logic:
//These Are the Possible Answers
var solutions:Array = new Array("A","B","C","D");
function testInput(_inputText:String):Boolean
{
//the delimiter could also be a comma, here it is a space
var inputStringArray:Array = _inputText.split(" ");
var counter:Number = 0;
for (var i:int=0; i<inputStringArray.length; i++)
{
for (var j:int=0; j<solutions.length; j++)
{
if (inputStringArray== solutions
{
...
Copy link to clipboard
Copied
// Register an event listener that detects when something is written in the TextField
Ex.addEventListener(TextEvent.TEXT_INPUT, answerTxtInp);
// The function that will be called by the event listener
function answerTxtInp(txtEvent:TextEvent):void
{
// depending on the possible answers of characters, change the border color
Ex.borderColor = 0x00FF00;
}
Copy link to clipboard
Copied
what if they enter 3 "words" two of which are correct and one of which is not correct? is their answer correct?
Copy link to clipboard
Copied
The solution of Nishu with Evaluation Logic:
//These Are the Possible Answers
var solutions:Array = new Array("A","B","C","D");
function testInput(_inputText:String):Boolean
{
//the delimiter could also be a comma, here it is a space
var inputStringArray:Array = _inputText.split(" ");
var counter:Number = 0;
for (var i:int=0; i<inputStringArray.length; i++)
{
for (var j:int=0; j<solutions.length; j++)
{
if (inputStringArray== solutions
{
counter++;
}
}
}
if (counter >=2)
{
trace("true");
return true;
}
else
{
trace("false");
return false;
}
}
//Textfield with name input_txt on stage
input_txt.addEventListener(TextEvent.TEXT_INPUT, answerTxtInp);
// The function that will be called by the event listener
function answerTxtInp(txtEvent:TextEvent):void
{
// depending on the possible answers of characters, change the border color
if(testInput(input_txt.text)){
input_txt.borderColor = 0xFF0000;
}
else{
input_txt.borderColor = 0x000000;
}
}
Copy link to clipboard
Copied
Actually one can do it in a much more streamlined way. The code below also can handle case sensitivity unlike the code suggested by moccamaximum. You can also toggle case sensitivity depending on your needs:
// number of minimum answers
var minAnswers:int = 2;
var solutions:Array = ["blah", "bird", "caR", "Cow"];
// create regular exprression out of the array of solutions
var solutionsRE:RegExp = new RegExp("\\b(" + solutions.join("|") + ")\\b", "gi");
input_txt.addEventListener(TextEvent.TEXT_INPUT, answerTxtInp);
function answerTxtInp(e:TextEvent):void
{
// depending on the possible answers of characters, change the border color
input_txt.borderColor = answersRE.test(input_txt.text) && input_txt.text.match(answersRE).length >= minAnswers ? 0xFF0000 : 0x000000;
}
Copy link to clipboard
Copied
Oh, and forgot to mention.
Approach I suggested allows for multi-word solutions.
If solutions array is ["blah", "bird", "car", "cow", "making things", "New York"]
If person enters, say, "New York" - code will detect that answer is correct. But if person enters just "New" or "York" - answer will not be treated as a correct one.
Copy link to clipboard
Copied
Now there is only one small issue:
How do we prevent that the user cheats by inputting for example "blah blah " ?
Copy link to clipboard
Copied
that's why i asked: what if they enter 3 "words" two of which are correct and one of which is not correct? is their answer correct?
evalF will return [correct number,incorrect number] when passed the user's string (with each answer seperated by a delimiter) and an array of correct answers (in lower case):
function evalF(s:String,answersA:Array):Array {
var correct:int = 0;
var incorrect:int = 0;
// arrayF should be passed the users delimiter as the 2nd parameter
var answerA:Array = arrayF(s,",");
for (var i:int=0; i<answerA.length; i++) {
if(answersA.indexOf(answerA.toLowerCase())>-1){
correct++;
} else {
incorrect++;
}
}
return [correct,incorrect];
}
function arrayF(s:String,delimiter:String):Array {
var a:Array=s.split(delimiter);
for (var i:int=0; i<a.length; i++) {
var char:String=a.substr(a.length-1,a.length-1);
while (char==" ") {
a=a.substr(0,-1);
char=a.substr(a.length-1,a.length-1);
}
char=a.substr(0,1);
while (char==" ") {
a=a.substr(1);
char=a.substr(0,1);
}
}
return a;
}
Copy link to clipboard
Copied
The code bellow doesn't allow for duplicates:
// number of minimum answers
var minAnswers:int = 2;
var solutions:Array = ["blah", "bird", "caR", "Cow", "New York"];
// create regular exprression out of the array of solutions
var solutionsRE:RegExp = new RegExp("\\b(" + solutions.join("|") + ")\\b", "gi");
var answers:Array;
input_txt.addEventListener(Event.CHANGE, answerTxtInp);
function answerTxtInp(e:Event):void
{
answers = [];
answers = input_txt.text.match(solutionsRE).filter(removeDuplicates);
// depending on the possible answers of characters, change the border color
input_txt.borderColor = answers.length >= minAnswers ? 0xFF0000 : 0x000000;
}
function removeDuplicates(element:*, index:int, arr:Array):Boolean
{
return answers.indexOf(element) > -1 ? false : answers.push(element);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now