From d8bb137ad28cc7c46c564ff06c2f47183b15c233 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Sun, 15 Jan 2023 15:44:40 +0100 Subject: [PATCH] Strachey 0 --- 2.Ghost-Writing.md | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/2.Ghost-Writing.md b/2.Ghost-Writing.md index f1cdc42..c55e82d 100644 --- a/2.Ghost-Writing.md +++ b/2.Ghost-Writing.md @@ -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 + +

+ +

+ +