Random color square JavaScript
Write a program that draws a random color square centered on the graphics canvas. The sides of the square should be SIDE_LENGTH.
(Hint: Use the Randomizer to get a random color for the square.)
What am I doing wrong?
var SIDE_LENGTH = 100;
function start(){
var square = new Rectangle (100,100);
var x = getWidth()/2;
var y = getHeight()/2;
square.setPosition(x,y);
var color = Randomizer.nextColor();
square.setColor(color);
add(square);
}
In the given code, there are a couple of things you might be doing wrong.
Firstly, make sure to include the required graphics library by adding the following line at the start of your code:
```
useLibrary('graphics');
```
Next, ensure that you have correctly defined the `Rectangle` class. To do this, you can use the `var` keyword to define a rectangle object with your desired dimensions.
Finally, be sure to call the `start()` function to execute the code and display the square on the canvas.
Here's an updated version of the code with these fixes:
```javascript
useLibrary('graphics');
var SIDE_LENGTH = 100;
function start(){
var square = new Rectangle(SIDE_LENGTH, SIDE_LENGTH);
var x = getWidth()/2;
var y = getHeight()/2;
square.setPosition(x, y);
var color = Randomizer.nextColor();
square.setColor(color);
add(square);
}
start();
```
With these modifications, the code should create a random color square with a side length of `SIDE_LENGTH` centered on the graphics canvas.