0

I have an array is of the form

var array = ["cat1", "cat2", "cat3"]

I would like to be able to randomly choose one of these elements, where each element is the actual name of another array.

So I also have

var cat1 = ["Q1", "Q2", "Q3"] basically an array of questions.

this is a similar format for cat2 and cat3.

Now the reason I want this is to randomly choose a topic for questions, then select a question from the chosen array, my problem is I don't know how to programmatically use, say for example,

cat2[1]

I basically want to display the contents of the element that is chosen.

Is this at all possible? I have been trying to get this to play nice for a while now :(

Thank you!

Ahmad F
  • 30,560
  • 17
  • 97
  • 143

5 Answers5

1
You can use it like this--

var cat1 = ["Q1", "Q2", "Q3"]

var cat2 = ["Q4", "Q5", "Q6"]

var cat3 = ["Q7", "Q8", "Q9"]

var array = [cat1, cat2, cat3]

print(array[0][0])

print(cat[1])

-------------------
the answer would be :--- Q1
MageNative
  • 682
  • 5
  • 14
0

Why don't you use array of arrays, like

var cat1 = ["Q1", "Q2"]
var cat2 = ["Q10", "Q11"]
var cat3 = ["Q20", "Q21"]
var array = [cat1, cat2, cat3]

And chose randomly one object from array and that one object will be one of cat1, cat2 or cat3 and that represents the array of questions


Edit: Seems that these are your instance variable either initialize them in viweDidLoad if it's in view controller or use the below code

var array = [["Q1", "Q2"],
             ["Q10", "Q11"],
             ["Q20", "Q21"]]
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

type can either use a [[String]] or create a custom class for that.

To use a [[String]], first declare the arrays cat1, cat2 and cat3:

let cat1 = [...]
let cat2 = [...]
let cat3 = [...]

Then you simply do this:

let array = [cat1, cat2, cat3] // without the ""s!

Refer to this post for how to select a random item.

You basically select a random item from array, put the result into an array called randomTopic or whatever, then choose another random item from randomTopic.


Alternatively, you can create a type to store questions.

struct Topic {
    let questions: [String]
}

Then you create an array of Topic i.e. [Topic] and do the same thing.

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

You can have an array of arrays and not Strings. For example:

var cat1 = ["Q1", "Q2", "Q3"]
var cat2 = ["Q1", "Q2", "Q3"]
var cat3 = ["Q1", "Q2", "Q3"]

var array = [cat1, cat2, cat3]

Or a two-dimensional array:

var array = [["Q1", "Q2", "Q3"], ["Q1", "Q2", "Q3"], ["Q1", "Q2", "Q3"]]

Then you can randomly choose a "line" and then the question from it:

var question = array[randomNumber][questionNumber]

Another alternative is to use a Dictionary. Then you will have this structure:

var dict = ["cat1" : ["Q1", "Q2", "Q3"],
            "cat2" : ["Q1", "Q2", "Q3"],
            "cat3" : ["Q1", "Q2", "Q3"]]
Max Pevsner
  • 4,098
  • 2
  • 18
  • 32
  • I triedvar array = [["Q1", "Q2", "Q3"], ["Q1", "Q2", "Q3"], ["Q1", "Q2", "Q3"]] but when I try print(array[0][0]) I get ["Q1","Q2","Q3"] – LeeMckelvey May 16 '17 at 08:46
0

Please below code and check the demo too.

/* DECLARING ALL THE QUESTION ARRAYS */
var cat1 = ["Q1", "Q2", "Q3", "Q4", "Q5"],
    cat2 = ["Q6", "Q7"],
    cat3 = ["Q8", "Q9", "Q10", "Q11", "Q12", "Q13"],
        /* DECLARING ALL THE QUESTION ARRAYS INTO ANOTHER SINGLE ARRAY */
        array = [cat1, cat2, cat3];

/* SELECTING WHICH QUESTION ARRAY TO PICK EITHER CAT1, CAT2 OR CAT3 USING MATH RONDOM FUNCTION */
var whichQuestionArray = array[Math.floor(Math.random() * array.length)],
    /* PICKING WHICH QUESTION TO SHOW FROM ABOVE ANSWER */
    selectedQuestion = whichQuestionArray[Math.floor(Math.random() * whichQuestionArray.length)];
/* PRINTING THE OUTPUT IN THE CONSOLE */
console.log("Selected Question - " + selectedQuestion);

Demo click here

Faizal
  • 250
  • 1
  • 8