Nested Variable

Is it possible to use a variable to define another variable? I am trying to pick a random question to display from a list of questions but I don’t want to use a “when” statement because I want to easily add more questions without editing a “when” statement to include the new values. Code attempt below:


quantity = 3

r=randomGenerator()
num = r.int(1,quantity)
qNum = “q${num}”
aNum = “a${num}”

question1 = ${"${qNum}"}
answer1 = ${"${aNum}"}

q1 = “Here is question 1”
a1 = “Here is answer 1”
q2 = “Here is question 2”
a2 = “Here is answer 2”
q3 = “Here is question 3”
a3 = “Here is answer 3”

Can’t really do that with text unfortunately. Essentially this is your only solution:

You can dynamically change things in text, so if you just want to change numbers it’s not too difficult.

Is it possible to make a list consisting of strings and then pull a random string?

No, lists can only contain points, numericvalues, or expressions. Referencing any of those things will only display numeric values, undefined, or NaN.

Would there be another way to write a list of 100 questions (strings) and then pull a random string without using a “when” statement with 100 whens?

I tried making a table but calling cell content does not allow for variables in the parameters.

There is no way to make and reference a list of strings, only numeric values. As I said, your only option is the unwieldy when-otherwise statements.

Off-topic a little bit but I think text ‘objects’ (so they can be stored in lists, accessed dynamically as labels, etc.) and text manipulation would be one of the items, if not the item, at the top of my Desmos feature wishlist.

1 Like

There isn’t any way to access them from the list though. You can dynamically define labels, but it is more to do with the numeric values being dynamically set than a text string itself. I also would like more text manipulation, and more dynamic ways of dealing with tables.

Not currently but that’s kind of my point. It could streamline so many processes if you could store and access text in a list - eg. imagine having L=["alpha","beta","gamma","delta","epsilon"] and being able to just set up P=(0,[5...1]) with the label L. This would be far more efficient than manually creating the five points and editing each label.

Similarly, if we could use something like p_lural={n>1:"apples","apple"} and set up the label to be ${n} ${p_lural}, it opens up a world of dynamic labelling beyond what’s currently possible/easy to implement.

I agree. I just didn’t expect anything any time soon, when you currently can’t even create and access lists of numeric data within the CL.

This is what I came up with as a workaround. I don’t think there’s a way to avoid a lot of when’s. random table row • Activity Builder by Desmos

Just reading through this and what about this work around:

Create a linear function and then create points along the linear function using any increment : e.g (0,0), (10,10), (20,20) etc.

  • label each of those points with the question you want to ask.
  • randomly generate a number between 1 and however many points you have.
  • Make sure the graph component is displayed and bounds are set to within +/- 2 (or whatever appropriate) of that question (on the x and y) with the focal point being that point on the line"
  • The graph component will then display the labelled point +/- however large the window you want is.

I think if you do it that way you just add questions ad hoc whenever you want to in the graph component and then increase the interval in which you want to generate random numbers with.

ok, so I had some time to throw something together:

Here is the link Random Question Generator • Activity Builder by Desmos

The questions are tied to points that are every 10 units along a horizontal line.
Ditto with the answers

To add a questions/answers you change the end of the list “p_x” to whatever is appropriate (eg 17 questions need 160)
Add additional points using notation p1[“question number”] and label with question (ditto with answers except p2 is the answer line for points).

@MrClark , @Daniel_Grubbs @Daniel_Wekselgreene

Hmm… that’s a really clever idea! Though I’m wondering if it’s actually less time consuming to make than using the whens? Since text is not supported well, I guess any solution is going to have some grindy nature to it.

I agree. I think it may take more processing time from a coding perspective, but it’s probably pretty negligible. I find it easier to read the CL over the graph expressions, and would probably choose when-otherwise for that reason alone.

The scalability is definitely limited by the way strings/text output on labelled points.

I just tried to do another work around where I had named note components (q1, q2, etc) and pulled one of them randomly on another page through random number generation but even that is not possible as soon as something is in a “string” in desmos, it can’t be used for object dot notation.

If I type: q1.script.question - it will pull that variable

however when I tried:
number=r.int(1,10)
reference=“q${number}”
reference.script.question - this didn’t work :confused:

B

I’m just hoping that they will figure out a way to let lists store latex strings. That would be a game changer.

1 Like

Also - here’s another variation of the above

Random Question Generator • Activity Builder by Desmos (same link as before)

I used “pointlabel”. This way would allow all the text/latex to be written in the CL layer. Probably a bit more user friendly. It’s just for the “answers” right now so click show answer and you’ll see the changed approach.

In terms of building this from ground up, the upfront time consumption would be manually inputting all the points and then after that adding them adhoc to the CL layer as needed and changing the randomly generated number range.

Instead of setting the length, l, by list p_x. I would define the length in the CL, and define the list in the graph as p_x=[0,10...10*(l-1)]. (Gosh I dislike l as a variable). Then, I would pre-define the p’s and q’s up to 30 or whatever I think is a reasonable number of questions for a bank (and note that cap in the CL). That way I don’t need to edit the graph as well, just the pointLabels and the number of questions, l, in the CL.

I would also pair the questions with answers in the CL for ease of additional entry (i.e. alternate points p1, q1, p2, q2…)

1 Like