Both sides previous revision Previous revision | |
p5js-week-11 [2022/07/08 10:19] – renick | p5js-week-11 [2023/06/29 22:56] (current) – reina.chen |
---|
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 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: | An <color black/pink>if statement</color> has three parts usually. The first part is the <color black/pink>condition</color>. 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) | if (toasterPluggedIn == true) |
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 "execute", they mean "carry out" or "do". The second part should be within curly brackets. In the code above, it's this part: | In the case of true, the function executes the second part of the statement. When programmers say <color black/pink>"execute"</color>, 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!")} | {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: | If the condition is false, the third part of the if statement is executed. It has the <color black/yellow>"else"</color> 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.")} | else {console.log("Please plug in the toaster.")} |
| |
Remember: | Remember: |
- After writing "if", put a test inside of parentheses. The test should evaluate to "true" or "false". | - After writing <color black/yellow>"if"</color>, put a test inside of parentheses. The test should evaluate to "true" or "false". |
- 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 "else" to divide the if-statements into two parts. | - Then we write <color black/yellow>"else"</color> to divide the if-statements into two parts. |
- After "else", put curly braces, and inside the curly braces write what you want JavaScript to do when the test is false. | - After <color black/yellow>"else"</color>, put curly braces, and inside the curly braces write what you want JavaScript to do when the test is false. |
| |
===== a second argument in a forEach callback ===== | ===== a second argument in a forEach callback ===== |
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/pink>forEach</color> on an array, and the argument we give it is a function, which we call a <color black/pink>callback</color>. We've done callbacks with one argument, in which the argument represents an item from the array, like this: |
| |
let someToast = ['slice0','slice1','slice2']; | let someToast = ['slice0','slice1','slice2']; |
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. | 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. | In an <color black/pink>arrow</color> 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. | 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. |