exercise 2.2
This commit is contained in:
parent
51c5be2bfc
commit
8ccd8d0cf7
@ -37,7 +37,7 @@ function keyTyped() {
|
||||
0 1 2 3 4 5 <--- indices (var INDEX)
|
||||
```
|
||||
|
||||
### 📚 Exercice
|
||||
### 📚 Exercise
|
||||
|
||||
1. Modify sketch 2.0 so that:
|
||||
* When key ```'k'``` is typed then ```"Hi Karin!"``` is printed on the console
|
||||
@ -102,7 +102,7 @@ i++
|
||||
//is the incremental (update) statement that is executed at the end of the loop (i.e. just after the loop statement [do_something();] as been executed)
|
||||
```
|
||||
|
||||
### 📚 Exercice
|
||||
### 📚 Exercise
|
||||
|
||||
1. Modify skecth 2.1 so that every one listed in the ```NAMES``` array have all the activities listed in the ```ACTIVITIES``` array be printed out on the console when key ```'x'``` is typed (this is called a nested loop!). The console output should look something like:
|
||||
|
||||
@ -172,7 +172,7 @@ let random_index_activities = Math.floor(random(ACTIVITIES.length));
|
||||
print(NAMES[random_index_names] + " likes to play " + ACTIVITIES[random_index_activities]);
|
||||
````
|
||||
|
||||
This will work, but it is not "elegant"... We have here two variables (random_index_names, random_index_activities) that are assigned a random value with the respective arrays' length (NAMES.length, ACTIVITIES.length). What if we had many arrays? We would need as many variables as there are arrays to select from right? The code would be very long!
|
||||
This will work, but it is not "elegant"... We have here two variables (```random_index_names```, ```random_index_activities```) that are assigned a random value with the respective arrays' length (```NAMES.length```, ```ACTIVITIES.length```). What if we had many arrays? We would need as many variables as there are arrays to select from right? The code would be very long!
|
||||
|
||||
To remediate this tedious need to write too much code, how about writing a function that selects a random element from an "generic" array?
|
||||
|
||||
@ -186,6 +186,8 @@ function choice(array_to_choose_from) {
|
||||
}
|
||||
````
|
||||
|
||||
|
||||
|
||||
```javascript
|
||||
// Array + Index + Randomness!
|
||||
|
||||
@ -215,3 +217,24 @@ function keyTyped() {
|
||||
// Reference: https://p5js.org/reference/
|
||||
````
|
||||
|
||||
### 📚 Exercise
|
||||
|
||||
1. Modify sketch 2.2 by adding a day of the week when an activity is taking place. For example:
|
||||
|
||||
```
|
||||
David likes to play records with Karin on Tuesday
|
||||
```
|
||||
|
||||
2. Come up with your own random sentences! Remember you can add as many arrays to select from as you want and most languages are structured "systems" having nouns, verb, adjectives, adverbs, etc. Here is what I came up with (can you guess what my code looks like?):
|
||||
|
||||
```
|
||||
Laura wears a blue sweater when Sigrid plays tennis
|
||||
Karin wears a black cap when Karin plays records
|
||||
Laura wears a yellow scarf when Nanna plays piano
|
||||
Karin wears a yellow jacket when Sigrid plays chess
|
||||
Nanna wears a orange sweater when Nanna plays tennis
|
||||
Karin wears a black jacket when Maaike plays chess
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user