From 946306b7273ba64f4fcdc157581d8f02d9e2c625 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Sun, 15 Jan 2023 14:19:02 +0100 Subject: [PATCH] in-text code --- 2.Ghost-Writing.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/2.Ghost-Writing.md b/2.Ghost-Writing.md index 8210f77..a0912c3 100644 --- a/2.Ghost-Writing.md +++ b/2.Ghost-Writing.md @@ -40,13 +40,13 @@ function keyTyped() { ### 📚 Exercice 1. Modify sketch 2.0 so that: -* When key 'k' is typed then "Hi Karin!" is printed on the console -* When key 's' is typed then "Hi Sigrid!" is printed on the console -* When key 'n' is typed then "Hi Nanna!" is printed on the console -* When key 'm' is typed then "Hi Maaike!" is printed on the console +* When key ```'k'``` is typed then "Hi Karin!" is printed on the console +* When key ```'s'``` is typed then "Hi Sigrid!" is printed on the console +* When key ```'n'``` is typed then "Hi Nanna!" is printed on the console +* When key ```'m'``` is typed then "Hi Maaike!" is printed on the console 2. Modify sketch 2.0 so that: -* Every time key 'x' is typed then print the name of the following person in the array ("Hi David!", "Hi Karin!", "Hi Sigrid!", and so forth) +* Every time key ```'x'``` is typed then print the name of the following person in the array ("Hi David!", "Hi Karin!", "Hi Sigrid!", and so forth) ### 🤔 Who/what is "undefined"? @@ -104,7 +104,7 @@ i++ ### 📚 Exercice -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: +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: ``` Karin likes to play piano @@ -125,9 +125,9 @@ Nanna likes to play chess Randomness in p5js is based on generating a random number between 0 and a specified maximum. -Typically a random number is produced with the following statement: +Typically a random number is produced with the ```random``` function: ```javascript -random(X); // picks a random number between 0 and X +random(X); // generates a random number between 0 and X ```` Here is an example: ```javascript @@ -140,7 +140,9 @@ Rudimentary indeed, but there are many things we can build with this simple func Since we access elements of an array with indices, we can generate a random index between 0 and the length of an array so to select a random element in it. -The first problem we have to solve is that random gives 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: +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: ```javascript let random_number = Math.floor(random(10)); // assign a random integer number between 0 and 10 to variable random_number @@ -156,9 +158,9 @@ let random_index_names = Math.floor(random(NAMES.length)); print(NAMES[random_index_names]); ```` -This will indeed print a random name in the array NAMES each time it is executed. +This will indeed print a random name in the array ```NAMES``` each time it is executed. -But what if we would like to print a random activity from the ACTIVITIES array as well? +But what if we would like to print a random activity from the ```ACTIVITIES``` array as well? ```javascript var NAMES = ["David", "Karin", "Sigrid", "Nanna", "Laura", "Maaike"];