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/08 09:17] – renick | p5js-week-11 [2023/06/29 22:56] (current) – reina.chen | ||
---|---|---|---|
Line 5: | Line 5: | ||
(photo from https:// | (photo from https:// | ||
- | Now that we've reviewed what we've learned, let's learn one more fundamental part of JavaScript. | + | 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: | There are a couple of things we want to study today: | ||
Line 15: | Line 17: | ||
===== if-statements ===== | ===== if-statements ===== | ||
- | Let's learn another key word for the JavaScript | + | {{: |
+ | |||
+ | (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 25: | 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 31: | 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(" | ||
Line 50: | Line 58: | ||
===== 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 73: | 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 ===== | ===== a second argument in a forEach callback ===== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | (photo from https:// | ||
Remember back in week 06 that we studied how to use forEach on arrays. | 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: | + | We call <color black/ |
let someToast = [' | let someToast = [' | ||
Line 97: | Line 113: | ||
There' | There' | ||
- | In an arrow function, JavaScript makes it easy when 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, | 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, | ||
Line 103: | Line 119: | ||
===== using if-statements in p5js ===== | ===== 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! Check the comments in the code to understand what the if-statement is doing. | + | 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=" | <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. | ||