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 10:13] – renick | p5js-week-11 [2023/06/29 22:56] (current) – reina.chen | ||
---|---|---|---|
Line 33: | 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 39: | 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 85: | 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 ===== | ||
Line 98: | Line 98: | ||
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 113: | 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 119: | 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. | ||
< | < |