Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
p5js-week-07 [2022/06/11 08:15] renickp5js-week-07 [2023/06/26 00:48] (current) reina.chen
Line 59: Line 59:
   - **generalization**   - **generalization**
  
-When we make a function which contains a procedure, we are using **encapsulation**.+When we make a function which contains a procedure, we are using <color black/pink>encapsulation</color>.
  
 <code>  <code> 
Line 71: Line 71:
 </code> </code>
  
-When we add arguments to our function so that the function can be used in more cases, we are using **generalization**.+When we add arguments to our function so that the function can be used in more cases, we are using <color black/pink>generalization</color>.
  
 <code>  <code> 
Line 83: Line 83:
 </code> </code>
  
-We are also using **generalization** when we structure our data into a class and use a constructor with arguments.+We are also using <color black/pink>generalization</color> when we structure our data into a class and use a constructor with arguments.
  
 <code> <code>
Line 130: Line 130:
 ====making functions, including using arrow functions==== ====making functions, including using arrow functions====
  
-We make functions starting with the "function" keyword, followed by the function name, then the arguments, and finally the body of the function inside of curly braces.+We make functions starting with the <color black/yellow>"function"</color> keyword, followed by the function name, then the arguments, and finally the body of the function inside of curly braces.
  
-Remember, the arguments are the input to our function:+Remember, the <color black/pink>arguments</color> are the input to our function:
  
 <code> <code>
Line 141: Line 141:
 </code> </code>
  
-Don't forget the return to get some output from the function!+Don't forget the <color black/yellow>return</color> to get some output from the function!
  
-We can also make functions in the arrow function style. One way is to assign it to a variable:+We can also make functions in the <color black/pink>arrow</color> function style. One way is to assign it to a variable:
  
 <code> <code>
Line 176: Line 176:
 ====various operators==== ====various operators====
  
-We've practice several operators like all of the ones for arithmetic (+, -, *, / ), and the remainder operator (%) and increment operator (++) as well.+We've practice several operators like all of the ones for arithmetic (+, -, *, / ), and the <color black/pink>remainder operator</color> (%) and <color black/pink>increment operator</color> (++) as well.
  
 We can use them to do math anywhere, like this: We can use them to do math anywhere, like this:
Line 186: Line 186:
 ====using booleans (true and false)==== ====using booleans (true and false)====
  
-One operator that we learned about when we studied for-loops is the  > (greater than) operator. There are also the < (less than) operator, the >= (greater than or equal) operator and the <= (less than or equal) operator. There's also the == (equal) operator. Try these:+One operator that we learned about when we studied for-loops is the  <color black/pink>> (greater than) operator</color>. There are also the <color black/pink>< (less than) operator</color>, the <color black/pink>>= (greater than or equal) operator</color> and the <color black/pink><= (less than or equal) operator</color>. There's also the <color black/pink>== (equal) operator</color>. Try these:
  
 <code> <code>
Line 196: Line 196:
 ====structuring data using arrays, and using push, pop, and unshift to add and remove items from those arrays==== ====structuring data using arrays, and using push, pop, and unshift to add and remove items from those arrays====
  
-We can collect data and put it into structure. One very important structure is arrays. We use push and unshift to add data to arrays. Let's do that for everyone's breakfast orders:+We can collect data and put it into structure. One very important structure is <color black/pink>arrays</color>. We use <color black/pink>push</color> and <color black/pink>unshift</color> to add data to arrays. Let's do that for everyone's breakfast orders:
  
 <code> <code>
Line 212: Line 212:
 </code> </code>
  
-Don't forget that you can get items from an array using the indexes. Each position in an array has a number, starting with 0.+Don't forget that you can get items from an array using the <color black/pink>indexes</color>. Each position in an array has a number, starting with 0.
  
 <code> <code>
Line 222: Line 222:
 ====for-loops for repeating actions==== ====for-loops for repeating actions====
  
-We have learned how to make for-loops to do things repeatedly. A for-loop has a first part in parentheses that describe the conditions for the for-loop: what number to start at (a counter), how long to run, and what to do with that counter. A for-loop has a second part in curly braces that says what you want to do on each loop.+We have learned how to make for-loops to do things repeatedly. A <color black/pink>for-loop</color> has a first part in parentheses that describe the conditions for the for-loop: what number to start at (a <color black/pink>counter</color>), how long to run, and what to do with that counter. A for-loop has a second part in curly braces that says what you want to do on each loop.
  
 We can use one with the things we've made above. Let's find out how much toast we have to make to fill everyone's order. We'll use the for-loop to get the value at each index with the counter i. We can use one with the things we've made above. Let's find out how much toast we have to make to fill everyone's order. We'll use the for-loop to get the value at each index with the counter i.
Line 244: Line 244:
 ====using forEach instead of a for-loop==== ====using forEach instead of a for-loop====
  
-We can rewrite totalToast using forEach instead. Remember that forEach is a method to be used on arrays. Instead of the code inside the for-loop above, we can use an arrow function.+We can rewrite totalToast using <color black/pink>forEach</color> instead. Remember that forEach is a method to be used on arrays. Instead of the code inside the for-loop above, we can use an arrow function.
  
 <code> <code>
Line 256: Line 256:
 ====structuring data using objects==== ====structuring data using objects====
  
-We have another way to structure toast, not just as arrays. That is objects. Let's make an object for each toast order.+We have another way to structure toast, not just as arrays. That is <color black/pink>objects</color>. Let's make an object for each toast order.
  
-There are properties on the left, each of which has a value on the right. Remember to separate the properties with commas. +There are <color black/pink>properties</color> on the left, each of which has a value on the right. Remember to separate the properties with commas. 
  
 <code> <code>
Line 270: Line 270:
 ====making classes so that making objects is easier and making those objects more useful==== ====making classes so that making objects is easier and making those objects more useful====
  
-Since we want to make a lot of objects like this, we can make a class. A class is like a factory for objects.+Since we want to make a lot of objects like this, we can make a class. A <color black/pink>class</color> is like a factory for objects.
  
 <code> <code>
Line 282: Line 282:
 </code> </code>
  
-When we want to use the class, we call the constructor with the "new" keyword. We can make all of the objects and then put them in an array like this:+When we want to use the class, we call the constructor with the <color black/yellow>"new"</color> keyword. We can make all of the objects and then put them in an array like this:
  
 <code> <code>
Line 386: Line 386:
 You can see after you run this a few times that the shapes created aren't always great when the Points are random. You can think about some ways to solve this. Still, this will be easy to use if you specify the Points. You can see after you run this a few times that the shapes created aren't always great when the Points are random. You can think about some ways to solve this. Still, this will be easy to use if you specify the Points.
  
-This sometimes makes what is called a self-intersecting polygon+This sometimes makes what is called a __self-intersecting polygon__
  
 https://en.wikipedia.org/wiki/Polygon#Convexity_and_non-convexity https://en.wikipedia.org/wiki/Polygon#Convexity_and_non-convexity
Line 396: Line 396:
 ===== algorithms ===== ===== algorithms =====
  
-We want either convex or concave polygons. We can write a set of rules to give us the output we want. We know one word for this: __procedure__. Another word we can use to call such sets of rules is __"algorithm"__+We want either convex or concave polygons. We can write a set of rules to give us the output we want. We know one word for this: <color black/pink>procedure</color>. Another word we can use to call such sets of rules is <color black/pink>"algorithm"</color>
  
 https://simple.wikipedia.org/wiki/Algorithm https://simple.wikipedia.org/wiki/Algorithm
Line 412: Line 412:
 ===== turning our algorithm into an abstraction ===== ===== turning our algorithm into an abstraction =====
  
-We want an abstraction, a function that implements this algorithm, which means to actually put the algorithm into a function so that we can run it.+We want an <color black/pink>abstraction</color>, a function that implements this algorithm, which means to actually put the algorithm into a function so that we can run it.
  
 In this case, we need some components. We'll need to: In this case, we need some components. We'll need to:
Line 458: Line 458:
 </code> </code>
  
-Now we can put these together to make a function that give us a list of points to be used to make a non-intersecting polygon. Notice that it uses the map method for arrays. +Now we can put these together to make a function that give us a list of points to be used to make a __non-intersecting polygon__. Notice that it uses the map method for arrays. 
  
 <code> <code>
Line 473: Line 473:
 ===== map ===== ===== map =====
  
-Like forEach, map is a method for arrays. The big difference is that map applies a function to each item in the array and then returns an array filled with the result of those function calls. Like forEach, it's common to use an arrow function with map. We often use it with a one-argument function. There are other possibilities which we might look at later.+Like forEach, <color black/pink>map</color> is a method for arrays. The big difference is that map applies a function to each item in the array and then returns an array filled with the result of those function calls. Like forEach, it's common to use an arrow function with map. We often use it with a one-argument function. There are other possibilities which we might look at later.
  
 For example, if we have an array of numbers, we can do some math with those numbers and get a new array. For example, if we have an array of numbers, we can do some math with those numbers and get a new array.
  • p5js-week-07.txt
  • Last modified: 12 months ago
  • by reina.chen