This is an old revision of the document!


p5js week 11

(photo from https://commons.wikimedia.org/wiki/File:French_toast_variaton_in_Guatemala.jpg)

You can make toast in a toaster, but you can also make toast on a grill or in a pan. For example, you might want to make French toast like in the photo above.

Now that we've reviewed what we've learned, let's learn one more fundamental part of JavaScript to expand what we're able to do.

There are a couple of things we want to study today:

  1. if-statements
  2. learn more about forEach

If you're studying if-statements today, then I want you to start one more new drawing. Study on, and check the instructions for a new drawing at the end.

(photo from https://www.flickr.com/photos/131260238@N08/16606727168)

If you push down the lever, the toaster should start. If the timer is finished, the toaster should pop out the toast. If you want sweeter toast, you should put some jam on it. “If” is really important for making toast!

“If” is also an important part of programming. Let's learn how to use it in JavaScript. Here's an example to get us started.

let toasterPluggedIn = true;

if (toasterPluggedIn == true)
  {console.log("First step towards toasting is complete!")}
  else {console.log("Please plug in the toaster.")}

Try that out in the console, then let's check out what's going on in that code.

An if statement has three parts usually. The first part is the condition. It's a test which will tell us either true or false. The test should be inside parentheses. It has to evaluate to true or false; that means that when all of the sub-parts are calculated, the final value is either true or false, not a number or a string or something else. In the code above, that's the part that says:

if (toasterPluggedIn == true)

Since the variable toasterPluggedIn has been assigned as true, this part of the code will evaluate to true.

In the case of true, the function executes the second part of the statement. When programmers say “execute”, they mean “carry out” or “do”. The second part should be within curly brackets. In the code above, it's this part:

  {console.log("First step towards toasting is complete!")}

If the condition is false, the third part of the if statement is executed. It has the “else” keyword first, and then it's followed by the last part of the if statement inside curly braces. In the code above, it's this part:

  else {console.log("Please plug in the toaster.")}

In the code above, we set the variable toasterPluggedIn to true, so this part doesn't run. However, if we change what it's assigned to and try the if-statement again, we can see this part run:

toasterPluggedIn = false;

if (toasterPluggedIn == true)
  {console.log("First step towards toasting is complete!")}
  else {console.log("Please plug in the toaster.")}

Sometimes you don't need the third part, like when you don't need to do anything if the test condition is false. In those cases, you can leave it out.

(photo from https://commons.wikimedia.org/wiki/File:OK._Toaster.jpg)

We might want to use that if-statement to check our toaster more often than once, so let's put it in a function to make that easier.

toasterPluggedIn = false;

function checkToasterPower (toasterPowerBoolean) {
  if (toasterPluggedIn == true)
    {console.log("First step towards toasting is complete!")}
    else {console.log("Please plug in the toaster.")}
  return toasterPowerBoolean
  }
  
checkToasterPower (toasterPluggedIn)

Every time we call the checkToasterPower function, it will use the if-statement that we've programmed.

To review, here's the basic pattern for an if statement; you can fill in the blanks:

if () {} else {}

Remember:

  1. After writing “if”, put a test inside of parentheses. The test should evaluate to “true” or “false”.
  2. The next part is curly braces. Inside the curly braces, put what you want JavaScript to do for you when the test is true.
  3. Then we write “else” to divide the if-statements into two parts.
  4. After “else”, put curly braces, and inside the curly braces write what you want JavaScript to do when the test is false.

(photo from https://www.freeimageslive.co.uk/free_stock_image/cheese-toasties-jpg)

Remember back in week 06 that we studied how to use forEach on arrays.

We call forEach on an array, and the argument we give it is a function, which we call a callback. We've done callbacks with one argument, in which the argument represents an item from the array, like this:

let someToast = ['slice0','slice1','slice2'];
someToast.forEach(t => console.log("Here's a slice of toast for you: " + t))

However, we can use two arguments. The second argument is the index of the item in the array. Remember, arrays have indices:

someToast[0]

We can use that number to do something. Here's an example:

someToast.forEach((t,i) => {console.log("Here's a slice of toast for you: " + t + ". It's the " + i + " item in the array.")})

There's one more tricky point here that we need to learn, and that's about arrow functions. You can review arrow functions in week 05.

In an arrow function, JavaScript makes it easy when there's only one argument. However, when we have two or more arguments, we need to put them in parentheses. When we do that, we also need to put the body of the function in curly braces. If we don't do that, JavaScript will be confused.

Above, the arrow function has two arguments, t and i. t is the item from the array, and i is the item's index. You can see the body of the function, containing console.log, is inside of curly braces.

Using the things we've learned above, we can modify one of our toast drawings from before. Let's turn the burnt toast into freaky pink toast!

After we make an array of toast, we'll check the color of each piece of toast. If it's a burnt piece of toast, represented by the color “saddlebrown”, we'll change it to the color “magenta”.

Check the comments in the code to understand what the if-statement is doing.

If you've made it this far, try using an if-statement to make a new drawing. In this drawing, be creative! Try drawing something that you haven't yet: fruit, insects, trains, or something totally beautiful or weird from your imagination.

Like the toast drawing above, make a drawing of 100 or more of something using your own class. In it, use an if-statement to change some of the objects. You might change them based on:

  1. their location
  2. their size
  3. their color

In the end, we should be able to see the results of the if-statement clearly, just like the freaky pink toast example above.

Be sure to post your drawing to your wiki page.

  • p5js-week-11.1657300759.txt.gz
  • Last modified: 22 months ago
  • by renick