Strachey 0

This commit is contained in:
gauthiier 2023-01-15 15:44:40 +01:00
parent 3e08c94efd
commit d8bb137ad2

View File

@ -142,10 +142,10 @@ Since we access elements of an array with indices, we can generate a random inde
The first problem we have to solve is that the ```random``` functions generates a "real" number (ex: 4.993319470244624, 1.9486631456631776, 7.1841821754535813, etc.) while the indices of an array are "integer" numbers (5, 2, 7, etc.).
We thus need to round the "real" numbers produced by random (4.993319470244624 -> 5, 1.9486631456631776 -> 2, 7.1841821754535813 -> 7). We do this with the ```Math.floor``` function:
We thus need to round the "real" numbers produced by random (4.993319470244624 -> 5, 1.9486631456631776 -> 2, 7.1841821754535813 -> 7). We do this with the ```floor``` function:
```javascript
let random_number = Math.floor(random(10)); // assign a random integer number between 0 and 10 to variable random_number
let random_number = floor(random(10)); // assign a random integer number between 0 and 10 to variable random_number
````
With this "fix" we can generate a random index (integer) between 0 and the length of a given array.
@ -153,7 +153,7 @@ With this "fix" we can generate a random index (integer) between 0 and the lengt
```javascript
var NAMES = ["David", "Karin", "Sigrid", "Nanna", "Laura", "Maaike"];
let random_index_names = Math.floor(random(NAMES.length));
let random_index_names = floor(random(NAMES.length));
print(NAMES[random_index_names]);
````
@ -166,8 +166,8 @@ But what if we would like to print a random activity from the ```ACTIVITIES``` a
var NAMES = ["David", "Karin", "Sigrid", "Nanna", "Laura", "Maaike"];
var ACTIVITIES = ["piano", "tennis", "chess", "records"];
let random_index_names = Math.floor(random(NAMES.length));
let random_index_activities = Math.floor(random(ACTIVITIES.length));
let random_index_names = floor(random(NAMES.length));
let random_index_activities = floor(random(ACTIVITIES.length));
print(NAMES[random_index_names] + " likes to play " + ACTIVITIES[random_index_activities]);
````
@ -181,7 +181,7 @@ Here is an example of a custom "choice" function taking an array as input and re
```javascript
// function selecting a random element from an array
function choice(array_to_choose_from) {
let random_index = Math.floor(random(array_to_choose_from.length));
let random_index = floor(random(array_to_choose_from.length));
return array_to_choose_from[random_index];
}
````
@ -204,7 +204,7 @@ function draw() {
// function selecting a random element from an array
function choice(array_to_choose_from) {
let random_index = Math.floor(random(array_to_choose_from.length));
let random_index = floor(random(array_to_choose_from.length));
return array_to_choose_from[random_index];
}
@ -230,11 +230,18 @@ David likes to play records with Karin on Tuesday
```
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
Laura wears a expensive scarf when Karin plays records
Karin wears a yellow jacket when Sigrid plays chess
Nanna wears a orange sweater when Nanna plays tennis
David wears a ridiculous scarf when Sigrid plays records
Karin wears a black jacket when Maaike plays chess
```
## Christopher Strachey's Love Letters
<p align="center">
<img src="https://git.le-club-des-sans-sujets.org/gauthiier/Revisiting-Concepts-Notations-Software-Art/src/commit/3e08c94efdcc19e2bf5f91db461292f3a34b3c47/img/Strachey-Mark1.png" width="100%"/>
</p>