5 Replies Latest reply on Jun 25, 2007 10:12 PM by tusharwadekar

    multiple answers accepted

    twistedpancreas Level 1
      Hi there helpful people,

      just a quickie.

      If I have some input text that will allow multiple answers (say two answers, ie "a" or "b"), how do I go about that?

      I've tried the code below to no avail and any help would be appreciated:

        • 1. multiple answers accepted
          NoRefund17
          ok well before i attempt to tackle this question i would like to point out i'm not that good at actionscript, my main programming knowledge comes from C++ and even that is limited. However, i THINK i might be able to help. At the very least this will give you something to play with untill one of the real coders is able to answer :P..lol. Hope it helps, and if this is compleatly wrong, i'm sorry.Here it goes


          To my knowledge, i wasnt aware you could assign dual values to a variable by useing ||. I dont belive this is possible :/? is it? Anyway, maybe try doing this. If you want to compare it to two possible answers try making your code this. MAYBE it will work, i'm not sure


          • 2. Re: multiple answers accepted
            tusharwadekar
            Here you go... just call validateInput function with the input parameter.

            • 3. Re: multiple answers accepted
              twistedpancreas Level 1
              ok thanks guys,

              NoRefund17's code make sense to me (since i just had the formatting wrong) and works, but i get a little lost in yours tusharwadekar (maybe because I need to read up on functions again when i get a chance, unless you want to do a quick explanation tusharwadekar).

              Thanks both of you anyhow.
              • 4. multiple answers accepted
                NoRefund17 Level 1
                ok il try my best to explain it^^. Here is a breakdown of tush's code (wich is a more complicated, but much better vertion of mine).Il break it down to you line by line. Not sure how much you know about programming so il dumb it down, please dont take offense if i seem like i'm making it too simple, and if i dont make it simple enough or if i ramble to much to make it clear i'm sorry :/..lol^^

                var ansrs:Array = ["a", "b"];
                this creates an array called ansrs. An array is a variable that can basicly many copy's of the same variable with the same name and are accesed by a placeholder number that is between the [ ]. So the code basicly makes two variables. One is "ansrs[0]" wich is now = to "a" and ansrs[1] wich is now equal to "b". Arrays always start at [0] and work their way up depending how deep you make the array.

                function validateInput(input:String):Void {
                this line basicly opens and creates a function. A function is basicly blocks of code that are created once, and then the code within it can be called multiple times throught your program just by useing the name. in this case "function" is used to tell actionscript he is going to make a function. "validateInput" is the name of the function. "(input:String)" basicly creates a variable and sais what type of data will be sent to this function when it is called. so for example when you call the function later in a program.
                validateInput("MyNameIsBob");
                that calls the function up and passes the string "MyNameIsBob" into the fucntion. Within the function everwhere that the variable "input" is used, you are basicly useing "MyNameIsBob" in its place. So calling the fucntion like we did above as
                ValidateInput("MyNameIsBob"); is basicly the same exact thing as saying "var input:String = "MyNameIsBob"; from within the funciton.

                The "void" part of the function sais that the function will not be returning data and the "{" opens the function. Everything between that opening bracket and the closing one will be part of the function.


                var isCorrect:Boolean = false;
                here he is creating a variable called "isCorrect" of type Boolean. A Boolean variable is a datatype that is either True Or False. Nothing else. It can not hold strings, or other data. Just true and false. Here in this declaration it is given the value of false.

                for (var i:Number = 0; i<ansrs.length; i++) {
                here he is creating a loop. From left to right it does this. "for" tells the compiler what type of look. "var i:Number = 0;" creates a variable to be used in the loop called "i" and of data type Number and is set to "0". "i<ansrs.length;" this part is the condition. Its saying run the code within the for loop over and over untill i<ansrs.length;. "i++" tells the for loop what to do to the variable everytime the for loop loops. In this case its saying to add 1 to i. So the first time this loops runs i will = 0, then the second time it will = 1, and etc etc tuntill the for loops condition in the middle is met. the pourpose of this for loop is to check the answered contained in the array that was created earlier. Instead of doing this for loop, you could also create 2 "if" statements to compare the answeres to the array. But what if your array was 800 variables big? Or you wanted to change the acceptable answers later from 2 answers to 10? This way you can search the entire array easily without much extra coding at all.

                if (ansrs == input) {
                isCorrect = true;
                break;
                }

                this if statemtn you should get since you used if statemet in your other one. IN short, its taking ansrs[0] (wich is = to "a" becuase we declaired it in the array at the top of the script. the reason it is checking ansrs[
                0] is becuase i is equal to 0 on this path, as i explained earlier when i was explaining the for loop. The "i" variable used within the for loop is the same "i" variable that was delcaired in the for loop above. I.E. "for (var i:Number = 0; i<ansrs.length; i++)"

                }
                if (isCorrect) {
                trace("Correct");
                } else {
                trace("Wrong");
                }
                }


                again, i think you understand this part since you used if statemtns before. But this is basicly seeing if the data from ansrs[0] (and on the next pass of the for loop ansrs[1]) are acceptable answeres. If its not it trace's "wrong" on the screen, if it is it traces "correct". O yeah, and just incase. The reason his if stament doesnt compare anything is becuase that datatype is a Boolean (wich means a true or false value). In an if statment usually you do something like
                if (a == b){
                //code to execute if a == b is true
                }
                so that if stament is asking "does a == b? yes or no? or true or false?". So in this vertion of the "if" statement we dont need to compare anything, becuase the boolean variable sais "true" or "false" directly in the variable. No comparison needed here.




                Thats the detailed explanation. (very detailed..lol). Here is a summary :P

                //Declairing an array that holds the possible correct choices
                var ansrs:Array = ["a", "b"];

                //declair the function that will check the choices
                function validateInput(input:String):Void {
                var isCorrect:Boolean = false;

                //Make a for loop that will check every single variable within the array of variables to see if the answer is correct
                for (var i:Number = 0; i<ansrs.length; i++) {
                if (ansrs == input) {
                isCorrect = true;
                break;
                }
                } //this bracket right before this comment ends the for loop. Once it is over it goes to the next if statemtns

                //these if statemnts trace if the asnwer is correct or wrong
                if (isCorrect) {
                trace("Correct");
                } else {
                trace("Wrong");
                }
                }




                • 5. Re: multiple answers accepted
                  tusharwadekar Level 1
                  Thanks NoRefund17.