# Print object names in red or blue

**URL:** <https://discourse.processing.org/t/print-object-names-in-red-or-blue/9778>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [March 30, 2019, 4:09am UTC](https://discourse.processing.org/t/print-object-names-in-red-or-blue/9778 "2019-03-30T04:09:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pawlisko](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@pawlisko](https://discourse.processing.org/u/pawlisko)\
**Post date:** [March 30, 2019, 4:09am UTC](https://discourse.processing.org/t/print-object-names-in-red-or-blue/9778/1 "2019-03-30T04:09:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 30, 2019, 4:27am UTC](https://discourse.processing.org/t/print-object-names-in-red-or-blue/9778/2 "2019-03-30T04:27:55Z")

</div>

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

> **[constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor)**
>
> The constructor method is a special method for creating and initializing an object created within a class.

  

> **[class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class)**
>
> The class declaration creates a new class with a given name using prototype-based inheritance.

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

```

---

<div class="post-metadata">

**Author:** ![pawlisko](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@pawlisko](https://discourse.processing.org/u/pawlisko)\
**Post date:** [March 30, 2019, 2:08pm UTC](https://discourse.processing.org/t/print-object-names-in-red-or-blue/9778/3 "2019-03-30T14:08:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 30, 2019, 3:20pm UTC](https://discourse.processing.org/t/print-object-names-in-red-or-blue/9778/4 "2019-03-30T15:20:14Z")

</div>

> [@pawlisko](#):
>
> Would there be any other solutions?

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

> **[new - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new)**
>
> The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

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

```
