I need help in voding. Can you please help me. I am trying to figure it it out gor 2 days

i am learning javascript from a book and it has some challenges. i cannot figure out one. can you please help me.

the challenge is to create a loop that would create an array of rectangles that their colour changed gradually from black to white( like a gradient). you should build in such a way that a single variable would control the number of rectangle drawn.
here is my code:

function setup() {
createCanvas(800, 300);

function draw() {

var space= 80

for(var i = 0; i<9;i=i+1){


i can make the rectangles but cannot do the gradient in them. hope you will help me do it. btw i am doing this on p5.js

I found this link that should help you with your project. Good luck!

Sorry but that didn’t helped me. I can only use some things such as a for loop, random function or noise function. If you want to help me, can you please check the book. Th book’s name is learn Javascript with p5.js , then go to chapter 7 and go to practice. There is a free PDF of the book. Thank you for your efforts to help me.