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!

6
  • is user will choose the cate1 cate2 etc? Commented May 16, 2017 at 6:42
  • 1
    i think the correct way is to use dictionary for your questions. Commented May 16, 2017 at 6:42
  • @LeeMckelvey you can use a dictionary where in each key will correspond to and array of question more like ["cat1" : ["Q1","Q2","Q3"], "cat2" : ["Q1","Q2","Q3"]]; Commented May 16, 2017 at 6:47
  • @Salman, no the user will not choose, it is to be random. Commented May 16, 2017 at 6:48
  • So i think you want to make dynamic variable right? Commented May 16, 2017 at 6:50

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

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"]]

1 Comment

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"]
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.

Comments

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"]]

3 Comments

When I try this it tells me "cannot use instance member 'cat1', within property initialiser; property initialisers run before 'self' is available."
Is there a specific way to write arrays inside arrays?
I've edited answer, is this code in a viewController then you can also create ivars as optionals and assign values in viewDidLoad
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.