Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
p5js-week-11 [2022/07/07 03:29] – renick | p5js-week-11 [2023/06/29 22:56] (current) – reina.chen | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== p5js week 11 ====== | ====== p5js week 11 ====== | ||
- | ===== if statement ===== | + | {{: |
- | Let's learn another key word for the JavaScript | + | (photo from https:// |
+ | |||
+ | 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 | ||
+ | |||
+ | There are a couple of things we want to study today: | ||
+ | - if-statements | ||
+ | - 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. | ||
+ | |||
+ | ===== if-statements ===== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | (photo from https:// | ||
+ | |||
+ | 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. " | ||
+ | |||
+ | " | ||
let toasterPluggedIn = true; | let toasterPluggedIn = true; | ||
Line 13: | Line 33: | ||
Try that out in the console, then let's check out what's going on in that code. | 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 we want to know is either true or false. The test should be inside | + | An <color black/ |
if (toasterPluggedIn == true) | if (toasterPluggedIn == true) | ||
Line 19: | Line 39: | ||
Since the variable toasterPluggedIn has been assigned as true, this part of the code will evaluate to 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 " | + | In the case of true, the function executes the second part of the statement. When programmers say <color black/ |
{console.log(" | {console.log(" | ||
- | If the condition is false, the third part of the if statement is executed. It has the " | + | If the condition is false, the third part of the if statement is executed. It has the <color black/ |
else {console.log(" | else {console.log(" | ||
- | | + | |
In the code above, we set the variable toasterPluggedIn to true, so this part doesn' | In the code above, we set the variable toasterPluggedIn to true, so this part doesn' | ||
Line 34: | Line 54: | ||
{console.log(" | {console.log(" | ||
else {console.log(" | else {console.log(" | ||
+ | |||
+ | 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. | ||
===== put an if-statement in a function ===== | ===== put an if-statement in a function ===== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | (photo from https:// | ||
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. | 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. | ||
Line 59: | Line 85: | ||
Remember: | Remember: | ||
- | - After writing " | + | - After writing |
- The next part is curly braces. Inside the curly braces, put what you want JavaScript to do for you when the test is true. | - The next part is curly braces. Inside the curly braces, put what you want JavaScript to do for you when the test is true. | ||
- | - Then we write " | + | - Then we write <color black/ |
- | - After " | + | - After <color black/ |
+ | |||
+ | ===== a second argument in a forEach callback ===== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | (photo from https:// | ||
+ | |||
+ | Remember back in week 06 that we studied how to use forEach on arrays. | ||
+ | |||
+ | We call <color black/ | ||
+ | |||
+ | let someToast = [' | ||
+ | someToast.forEach(t => console.log(" | ||
+ | |||
+ | 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, | ||
+ | |||
+ | There' | ||
+ | |||
+ | In an <color black/ | ||
+ | |||
+ | 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, | ||
+ | |||
+ | ===== using if-statements in p5js ===== | ||
+ | |||
+ | 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 " | ||
+ | |||
+ | Check the comments in the code to understand what the if-statement is doing. | ||
+ | |||
+ | < | ||
+ | <iframe src=" | ||
+ | </ | ||
+ | |||
+ | ===== your drawing using if-statements ===== | ||
+ | |||
+ | 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' | ||
+ | |||
+ | 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: | ||
+ | - their location | ||
+ | - their size | ||
+ | - 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. | ||