Skip to main content
Participant
March 26, 2013
Answered

Mathematical functions in an array

  • March 26, 2013
  • 1 reply
  • 724 views

Hi all;

I am building a multipul choise quiz.  There are two buttons, one with a value of "1" and the other with the value of "0".  These are being fed into an array called (Answers).  How do I add all the elements of the array together and then how do I perform mathematical functions on this new number to analys the percentage of correct answers and logically if the person has passed or not

thanks

James

This topic has been closed for replies.
Correct answer Ned Murphy

thankyou for your reply, that looks like a much more managable method to organise my quiz.  Just out of curiosity though, is there a way to "add" all the values in the array?


As I said in the first response you can simply loop thru the array and sum it as you go...

var sum = 0;

for (var I:uint=0; i<yourArray.length; i++){

     sum += yourArray;

}

1 reply

Ned Murphy
Legend
March 26, 2013

To add all the values in the array just loop thru it and keep a running sum, or just keep the running sum as you enter each value in to the array instead of adding them up at the end.

If you have a problem determining the percentage of correct answers and whether or not someone passes, then you need more than programming help.  Try to figure u=out how you would do it by hand first, then write code to do it instead.

If you show some code/effort in trying to get this working it will be easier to try to help you.

MMjameswAuthor
Participant
March 27, 2013

Hi there, thanks for your reply.\

Sorry i should have given my code!  I can get to the point where i have all the values in the array called answers.  I just want to know how to add all the values up.  As for the percentages (I do know how to calculate them) i was more looking for the mathematical codes.  Keep in mind this is the first time that i have programmed in something other then matlab so it may be rather bulky code.  Thank you for your time

stop();

//creating my Arrays
var nQNumber:Number = 0; //the question number
var aQuestions:Array = new Array(); //the questions that will appear on the screen

var rb1values:Array = new Array();
rb1values[0] = 0; //the values that the radio button 1 will take on each question
rb1values[1] = 0;
rb1values[2] = 0;
rb1values[3] = 0;
rb1values[4] = 0;
rb1values[5] = 1;
rb1values[6] = 0;
rb1values[7] = 0;
rb1values[8] = 0;
rb1values[9] = 1;
rb1values[10] = 1;
rb1values[11] = 1;
rb1values[12] = 0;
rb1values[13] = 0;
rb1values[14] = 0;

var rb2values:Array = new Array();
rb2values[0] = 0; //the values that the radio button 2 will take on each question
rb2values[1] = 1;
rb2values[2] = 0;
rb2values[3] = 0;
rb2values[4] = 0;
rb2values[5] = 0;
rb2values[6] = 0;
rb2values[7] = 1;
rb2values[8] = 0;
rb2values[9] = 0;
rb2values[10] = 0;
rb2values[11] = 0;
rb2values[12] = 0;
rb2values[13] = 0;
rb2values[14] = 0;

var rb3values:Array = new Array();
rb3values[0] = 0; //the values that the radio button 3 will take on each question
rb3values[1] = 0;
rb3values[2] = 0;
rb3values[3] = 1;
rb3values[4] = 0;
rb3values[5] = 0;
rb3values[6] = 0;
rb3values[7] = 0;
rb3values[8] = 0;
rb3values[9] = 0;
rb3values[10] = 0;
rb3values[11] = 0;
rb3values[12] = 1;
rb3values[13] = 0;
rb3values[14] = 1;

var rb4values:Array = new Array();
rb4values[0] = 1; //the values that the radio button 4 will take on each question
rb4values[1] = 0;
rb4values[2] = 1;
rb4values[3] = 0;
rb4values[4] = 1;
rb4values[5] = 0;
rb4values[6] = 1;
rb4values[7] = 0;
rb4values[8] = 1;
rb4values[9] = 0;
rb4values[10] = 0;
rb4values[11] = 0;
rb4values[12] = 0;
rb4values[13] = 1;
rb4values[14] = 0;

var answers:Array = new Array(); //The users answers

//the questions
aQuestions[0] = "If you get lost in M-Files, what do you do?";
aQuestions[1] = "What is Metadata?";
aQuestions[2] = "You can find documents in M-Files by searching:";
aQuestions[3] = "What is a shortcut to finding documents that you have recently viewed?";
aQuestions[4] = "How do you save an existing file into M-Files?";
aQuestions[5] = "True or False, you can save an Email into M-Files?";
aQuestions[6] = "when creating a new document in M-Files, do you:";
aQuestions[7] = "In a new documents metadata 'properties' box, what does the asterisk '*' mean when it is next to the property field?";
aQuestions[8] = "How do you check out a document?";
aQuestions[9] = "How many users can a document be 'Checked out' to?";
aQuestions[10] = "Can you log out of M-Files without checking the document back in?";
aQuestions[11] = "If you want to create a new document and you cant find the document type in M-Files, do you:";
aQuestions[12] = "How do you know you have an assignment?";
aQuestions[13] = "What do you do if you receive and assignment";
aQuestions[14] = "What ways can you 'complete' an assignment?";


questions_txt.text = aQuestions[nQNumber];

var rb1L:Array = new Array(); //Array's to change the lables on the radio button for each different question
var rb2L:Array = new Array();
var rb3L:Array = new Array();
var rb4L:Array = new Array();

//Lables for question one

rb1L[0] = "Log out then back in?";
rb2L[0] = "Ask the machinemonitor IT Coordinator for help?";
rb3L[0] = "Ask a colleague?";
rb4L[0] = "Click on the home button of the menu bar?"

//Lables for question two

rb1L[1] = "Information generated by the client";
rb2L[1] = "Searchable properties describing a document";
rb3L[1] = "The convention for naming machinemonitor documents";
rb4L[1] = "Processed information taken from a machine";

//Lables for question three

rb1L[2] = "Automatic document numbers";
rb2L[2] = "File name";
rb3L[2] = "Metadata and document contents";
rb4L[2] = "All of the above";

//Lables for question four

rb1L[3] = "Searching the same criteria that you did before?";
rb2L[3] = "The history button?";
rb3L[3] = "'Recently accessed by me' button?";
rb4L[3] = "'Roll back' button";

//Lables for question five

rb1L[4] = "File > Save as, when the document is open in the applicaiton";
rb2L[4] = "Right click on the closed file icon and click";
rb3L[4] = "Copy and paste or drag and drop into M-Files";
rb4L[4] = "All of the above";

//Lables for question six

rb1L[5] = "True";
rb2L[5] = "False";
rb3L[5] = "All of the above";
rb4L[5] = "Non of the above";

//Lables for question seven

rb1L[6] = "Exit, go to Microsoft word, create a blank document and then save it in M-Files?";
rb2L[6] = "make a copy of an existing document in M-Files and then type over the existing information and then 'Save as'";
rb3L[6] = "Select the 'New Document' link in the M-Files Master Panel";
rb4L[6] = "All of the above";

//Lables for question eight

rb1L[7] = "You don't have to fill in the property box";
rb2L[7] = "The information that is required by this field is mandatory";
rb3L[7] = "The information in this box is important";
rb4L[7] = "The information in this box is automatic";

//Lables for question nine

rb1L[8] = "Double click on the file and then select 'check out'";
rb2L[8] = "Select the 'check out' link from the left side 'view and modify' menu bar";
rb3L[8] = "Right click on the document and then select 'check out'";
rb4L[8] = "All of the above";

//Lables for question ten

rb1L[9] = "1 user at a time";
rb2L[9] = "2 users at a time as long as they are working in the same business area i.e. Engineering";
rb3L[9] = "Any amount of users";
rb4L[9] = "x-1, where x = the number of securities on the document";

//Lables for question eleven

rb1L[10] = "yes";
rb2L[10] = "no";
rb3L[10] = "maybe";
rb4L[10] = "Only if IT allow you";

//Lables for question twelve

rb1L[11] = "Create a new 'other document' type document in the metadata";
rb2L[11] = "Move on to your next task";
rb3L[11] = "Create a new document and save it to the desktop";
rb4L[11] = "Contact It to add it to the list";

//Lables for question thirteen

rb1L[12] = "By email";
rb2L[12] = "It's assigned to me in the M-Files Master panel";
rb3L[12] = "All of the above";
rb4L[12] = "Your manager will advise you on a regular basis";

//Lables for question fourteen

rb1L[13] = "Delete it and ignore it?";
rb2L[13] = "Wait and do it at the last minute?";
rb3L[13] = "let someone else worry about it?";
rb4L[13] = "click on the assignment, and action it?";

//Lables for question fifteen

rb1L[14] = "Assign it to someone else?";
rb2L[14] = "Click on the assignment, and tick the box next to your name";
rb3L[14] = "All of the above";
rb4L[14] = "Delete it";

//Inserting the lables into the radio buttons

rb1.label = rb1L[nQNumber];
rb2.label = rb2L[nQNumber];
rb3.label = rb3L[nQNumber];
rb4.label = rb4L[nQNumber];

//Adding the values to the radio buttons

rb1.value = rb1values[nQNumber];
rb2.value = rb2values[nQNumber];
rb3.value = rb3values[nQNumber];
rb4.value = rb4values[nQNumber];

//Import of the controlls for the radio buttons
import fl.controls.RadioButtonGroup;

submit_btn.addEventListener(MouseEvent.CLICK, quiz);
function quiz(e:MouseEvent):void
{
if(rb1.selected){
  answers.push(rb1.value)
}
if(rb2.selected){
  answers.push(rb2.value)
}
if(rb3.selected){
  answers.push(rb3.value)
}
if(rb4.selected){
  answers.push(rb4.value)
}
nQNumber++;
questions_txt.text = aQuestions[nQNumber];
rb1.value = rb1values[nQNumber];
rb2.value = rb2values[nQNumber];
rb3.value = rb3values[nQNumber];
rb4.value = rb4values[nQNumber];
rb1.label = rb1L[nQNumber];
rb2.label = rb2L[nQNumber];
rb3.label = rb3L[nQNumber];
rb4.label = rb4L[nQNumber];

}

Inspiring
March 27, 2013

Instead of creating multiple Arrays, which makes your quiz really hard to maintain, you should make one large Object, that holds all your data:

var quiz_obj:Object = new Object();

quiz_obj = [

            {_question:"What question?",

            _rb1:0,_rb2:0,_rb3:0,_rb4:0,

            _option1:"ANSWER 1",

            _option2:"ANSWER 2",

            _option3:"ANSWER 3",

            _option4:"ANSWER 4"},

           

            {_question:"Next question?",

            _rb1:0,_rb2:0,_rb3:0,_rb4:0,

            _option1:"ANSWER 1",

            _option2:"ANSWER 2",

            _option3:"ANSWER 3",

            _option4:"ANSWER 4"}       

           

            ];                     

trace(quiz_obj[0]._question +":"+quiz_obj[0]._option2);

//What question?:ANSWER 2