Print object names in red or blue

My professor assigned this assignment and I just can’t figure out how to do it. Can anyone help me out or at least give me a head start. I’ve been trying to figure it out all day today and I get more and more confused. Thank you

Create an array of objects. Each object should have the following properties:

  1. A name, stored as a string (text)
  2. A number from 0-255 (call it hue), populated using p5’s random function.
  3. A boolean (true/false) value (called redOrBlue)

Create a for loop that iterates over all of the objects in the above array. In each iteration of the for loop, print that object’s name. If that object’s redOrBlue flag is true, then the text should be red, with a value of that object’s hue property (eg fill(obj.hue, 0, 0); ). If the flag is false, then the text should be blue with a value of the object’s hue property ( fill(0, 0, obj.hue); ) All of the text names should appear on the screen at once.

Well, you can start off by defining a class that’ll be used to create your objects: :bulb:


class HueText {
  constructor(text, hue = ~~random(256), redOrBlue = random() < .5) {
    this.text = text;
    this.hue = hue;
    this.redOrBlue = redOrBlue;
  }
}
1 Like

i don’t think my professor wants me to use that method because we haven’t fully covered it yet.

would there be any other solutions?

Well, we can always use the ancient constructor function style: :flushed:

function HueText(text, hue = ~~random(256), redOrBlue = random() < .5) {
  this.text = text;
  this.hue = hue;
  this.redOrBlue = redOrBlue;
}