Below are some images to give you a workout using loops and if statements.
Your goal is to write functions in the
images-challenge environment
that create something like each of these images. However do not simply
write a bunch of calls to drawing functions like
drawFilledCircle
. Your functions should contain only one
call to any given drawing function in the body of a loop that does the
rest of the work.
Another thing you should do (and which I will be looking for when I review your code) is to develop your code incrementally. Write a simple version of the function that does some part of what you need and then build from there. Or if you can write the function all in one go on your first try, then try to simplify or otherwise improve your code.
A couple functions that might come in handy are a few more from
Math
.
Math.floor
takes a single numeric argument and returns the value rounded down to
the nearest whole number. And
Math.random
takes no arguments and returns a random number (pseudo-random,
technically) between 0, inclusive, and 1, exclusive.
The function should take a single argument that allows the caller to specify the size of the circles drawn. It should then draw as many as fit across the width of the canvas.
Like the previous function, this function should take a single argument that allows the caller to specify the size of the circles drawn. It should then draw as many as fit across the width of the canvas, alternating between two colors.
The function should take a single argument that specifies how many concentric circles to draw. It should then draw the largest circle to just fit in the canvas and the rest evenly spaced.
The function should take a single argument, `n`, that specifies the number of squares on each side of a checkeboard and should draw an n-by-n checker board.
The function should take a single argument that specifies the number of lines to draw.
The function should take a single argument that specifies the size of the circles and should then fill the canvas with as many as fit.
The function should take two arguments: the first specifies the size of
the circles and the second the probability that any given circle is
drawn filled in. For instance this image was creted with a circle radius
of 17 and a probability of 0.23. You will need to use the function
Math.random()
which returns a random number from 0.0 to
just below 1.0 each time it is called.
The function should take a single argument that specifies the size of the circles and should then draw the square made up of circles of that size.