# Problem with pulling values from array of colours, strings

**URL:** https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267
**Category:** Beginners
**Created:** [March 1, 2020, 6:23am UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267 "2020-03-01T06:23:26Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![leobrooks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/leobrooks/32/6686_2.png) [@leobrooks](https://discourse.processing.org/u/leobrooks)
#### Post date: [March 1, 2020, 6:23am UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/1 "2020-03-01T06:23:26Z")

</div>

Hello,

I’ve extracted my problem from a larger sketch, but the main problem is here.

When I run this, the colours and letters appear alright, but I get this error:  
Uncaught Error: [object Arguments]is not a valid colour representation. (sketch: line 17)

If I remove the fill() command, then I get this error:  
text() was expecting String|Object|Array|Number|Boolean for parameter #0 (zero-based index), received an empty variable instead. If not intentional, this is often a problem with scope:

I’m relatively new to coding, but can’t figure out why I can’t just pull these values from the arrays. I’m sure it’s something simple but I don’t know what I’m missing.

In the bigger sketch where I am using this, everything past this point in the draw function won’t work, so I’ve got to try and figure it out. Any help would be appreciated!

```auto
var colours = ['#CE2144', '#EC952F', '#FFFF00', '#90d40c', '#009287', '#554A8A', '#BC3984', '#CE2144'];
var letters = ["C", 'D', 'E', 'F', 'G', 'A', 'B', "C'"]
var start = 20

function setup() {
  createCanvas(400, 400);
  
}

function draw() {
  background(220);
  textSize(50)
  
  for (i = 0; i <= letters.length; i++) {
    fill(colours[i])
    text(letters[i], start, height/2);
    start = start+40;
  }
  
}

```

[https://editor.p5js.org/LeoBrooks/sketches/eE2JrW3e](https://editor.p5js.org/LeoBrooks/sketches/eE2JrW3e)

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [March 1, 2020, 8:23am UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/2 "2020-03-01T08:23:00Z")

</div>

> [@leobrooks](#):
>
> var colours = [‘#CE2144’, ‘#EC952F’, ‘#FFFF00’, ‘#90d40c’, ‘#009287’, ‘#554A8A’, ‘#BC3984’, ‘#CE2144’];

Your definition creates an array of strings. Usually anything in quotation marks is a string. And fill would like to have a number. Those color values are hexadecimals and in javascript they are preceded by 0x so the first value in your array would be 0xCE2144

---

<div class="post-metadata">

### Author: ![leobrooks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/leobrooks/32/6686_2.png) [@leobrooks](https://discourse.processing.org/u/leobrooks)
#### Post date: [March 1, 2020, 8:53am UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/3 "2020-03-01T08:53:51Z")

</div>

I tried replacing the array with numbers as 0xCE2144, 0xEC952F, etc but it didn’t work at all that way.

The error said:  
p5.js says: color() was expecting at least 1 arguments, but received only 0.

Anything I’m missing?

Leo

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [March 1, 2020, 9:28am UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/4 "2020-03-01T09:28:07Z")

</div>

I don’t get it. That web environment shows things right, but complains. Processing application with p5 mode just does things without errors.

[https://processing.org/reference/fill\_.html](https://processing.org/reference/fill_.html) says that RGB values with six digits must precede with # and RGBA values with eight digits must precede with 0x. So I gave you wrong guidance after all

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [March 1, 2020, 9:51am UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/5 "2020-03-01T09:51:11Z")

</div>

I feel so humbled. I was looking at the wrong place. Clearly I’m not used to error messages from javascript

`for (i = 0; i <= letters.length; i++) {`

loop causes overflow in indexing array and the error. It should be

`for (i = 0; i < letters.length; i++) {`

then for loop works. Try reducing what you add to start inside the loop and you’ll see some movement too

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 1, 2020, 12:01pm UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/6 "2020-03-01T12:01:04Z")

</div>

gotta repeat ` start = 20` imho

```auto

var colours = ['#CE2144', '#EC952F', '#FFFF00', '#90d40c', '#009287', '#554A8A', '#BC3984', '#CE2144'];
// var colours = [0xCE2144, 0xEC952F, 0xFFFF00, 0x90d40c, 0x009287, 0x554A8A, 0xBC3984, 0xCE2144];

var letters = ["C", 'D', 'E', 'F', 'G', 'A', 'B', "C"]

var start = 20

function setup() {
  createCanvas(400, 400);
  
}

function draw() {
  background(220);
  textSize(50)
  
  start = 20
  for (i = 0; i < letters.length; i++) {
    fill(color(colours[i]));
    text(letters[i], start, height/2);
    start = start+40;
  }
  
}

```

---

<div class="post-metadata">

### Author: ![leobrooks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/leobrooks/32/6686_2.png) [@leobrooks](https://discourse.processing.org/u/leobrooks)
#### Post date: [March 1, 2020, 4:37pm UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/7 "2020-03-01T16:37:24Z")

</div>

> [@Chrisir](#):
>
> var colours = [‘#CE2144’, ‘#EC952F’, ‘#FFFF00’, ‘#90d40c’, ‘#009287’, ‘#554A8A’, ‘#BC3984’, ‘#CE2144’]; // var colours = [0xCE2144, 0xEC952F, 0xFFFF00, 0x90d40c, 0x009287, 0x554A8A, 0xBC3984, 0xCE2144]; var letters = [“C”, ‘D’, ‘E’, ‘F’, ‘G’, ‘A’, ‘B’, “C”] var start = 20 function setup() { createCanvas(400, 400); } function draw() { background(220); textSize(50) start = 20 for (i = 0; i \< letters.length; i++) { fill(color(colours[i])); text(letters[i], start, height/2); start = start+40; }

Thanks so much to SomeOne and Chrisir for your help to fix this. But I don’t understand why “\<=” caused the loop to overflow? Wouldn’t “\<” stop the loop after 7 times instead of 8? Clearly this fixed the problem, but I feel like a moron for not understanding why.

Is it because the for loop starts at 0 so and Letters.length is 8 that it would actually run 9 times?

---

<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 1, 2020, 5:02pm UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/8 "2020-03-01T17:02:15Z")

</div>

> [@leobrooks](#):
>
> Is it because the for loop starts at 0 so and Letters.length is 8 that it would actually run 9 times?

That’s it! As another experiment, hit F12 to open browser’s console and paste this JS code below there:

```javascript
var ELEMENTS = 10, arr = Uint8Array.from({ length: ELEMENTS }, (v, k) => k);
console.table(arr);

for (var i = 0; i < ELEMENTS; console.info(i, arr[i++]));
console.log('\n');

for (var i = 0; i <= ELEMENTS; console.log(i, arr[i++]));

```

Notice the 2nd loop using `<=` logs the index 10 as `undefined`.

---

<div class="post-metadata">

### Author: ![leobrooks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/leobrooks/32/6686_2.png) [@leobrooks](https://discourse.processing.org/u/leobrooks)
#### Post date: [March 1, 2020, 5:11pm UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/9 "2020-03-01T17:11:36Z")

</div>

Very cool - Thanks!

Just goes to show that I still have so much to learn about coding.

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [March 1, 2020, 7:18pm UTC](https://discourse.processing.org/t/problem-with-pulling-values-from-array-of-colours-strings/18267/10 "2020-03-01T19:18:45Z")

</div>

We all do. I started programming 39 years ago and there’s always something new to learn. Some things get easier though.
