A Dash of Random explainer


#1

The aim of this puzzle: Draw two boxes with colors that are randomly generated.
Walkthrough of the solution: pickRandom() randomly selects a value from a list, while color holds a list of different colors. pickRandom(color) will randomly select one color, such as blue or purple. To make a box have a random color, pickRandom(color) needs to go inside drawBox(), like drawBox(pickRandom(color)). This is called Call Nesting; when a function — pickRandom() — is put inside another function — drawBox(). The function pickRandom(color) runs and a color is selected, this color is then “given” to the drawBox() function. If, for example, the random color selected was red, the computer would “see” drawBox(red) rather than drawBoxes(pickRandom(color)). This means a red box would be drawn.
Sample code solution:
(Tap below to reveal)

drawBox(pickRandom(color));
drawBox(pickRandom(color));

JavaScript Concepts: Calling Functions, Call Nesting, Identifiers
Grasshopper Concepts: pickRandom(), drawBox()


A dash of random
#2

I was a little confused by this one at first, but I assume this is happening:

var color = ['red', 'green', 'orange', 'yellow'];
function pickRandom(arr) {
  var index = Math.floor(Math.random() * arr.length);
  return arr[index];
}

#3

The instructions say “add a second box of random color”, the solution then displays 2 boxes initially. The solution is actually to add a third box. Would it not be better to rephrase the question to: “add another box of random color”?Screenshot_20180421-153928


#4

Thanks for the feedback!

You can also solve this puzzle by replacing the non-random colored box, with a drawBox(pickRandom(color)). But really appreciate your feedback on the wording, we’re looking at adjusting the instructions slightly for this puzzle.

H :slight_smile:


#5

Thanks very much, I didn’t consider that perspective as well.


#6

Oh God please make this stop. I was learning pretty good til I got here, Dash of Random. I swear I am doing exactly what it says and it’s like a frustrating circle of hell. It’s in a loop telling me I’m wrong but the explanation is harder than the question. I really want to continue with the course but I’m jammed up here.
Thanks for any help.
Why do I get two error messages that seem to contradict each other (red box mssg vs blue box mssg)? Do I add a color to Random Color or not?


#7

WooHoo!
Problem solved.
16-yr old stepsons said erase every thing that was there (the long line of code) and type in what I wanted.
Thanks