* 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)
//is the loop continuation condition: if the condition is true, the statement in the loop [do_something();] is executed, otherwise exit the loop when the condition is false
//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)
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:
let random_number = random(10); // assign a random number between 0 and 10 to variable random_number
````
Rudimentary indeed, but there are many things we can build with this simple function!
### 🤔 How to select a random element in an array?
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 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 ```floor``` function:
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!
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
One of the first non-scientific computer program that was ever written is Christopher Strachey's Love Letters program for the Manchester Mark I (so called [Baby](https://content.presspage.com/uploads/1369/1920_themanchestermk1computerbuiltbyextendingthebaby.jpg?10000)). In fact, this computer program (written in 1952 and making use of randomness) is arguably the first art-inspired program! Christopher Strachey was a computer programming pioneer who worked along Alan Turing in the very early days of computing at the University of Manchester. Strachey's story is fascinating. There is a great article about [him and the love letters on Rhizome's Queer History of Computing written by Jacob Gaboury](https://rhizome.org/editorial/2013/apr/9/queer-history-computing-part-three/). For those who are interested in researching Strachey's work, please have a look at his [papers and correspondences at the Bodleain library's archive](https://archives.bodleian.ox.ac.uk/repositories/2/resources/2561).
Strachey's Love Letters have been studied before. [David Link](http://www.alpha60.de) did a colossal reconstitution of the Love Letter program on a simulator of the Manchester Mark computer. [The works was exhibited circa 2010](http://www.alpha60.de/art/love_letters/). The program doesn't look at all like the code we are writing at the moment! The picture on the right side above (☝️) is from Link's simulator. Early computers did not have the compilers and interpreters we have now (remember compilers and interpreters) and thus the code was written in a rather cryptic way (believe me, way more cryptic than what we are writing).
The program we are about to write in a "modern" computing language (i.e. javascript/p5js) is a modified version of [Nick Montfort](https://nickm.com)'s letter.py source code written in python. Montfort's letter.py was based on Noah Wardrip-Fruin's article ["Digital Media Archaeology."](https://www.degruyter.com/document/doi/10.1525/9780520948518-016/html)
First, let's have a look at love letters to see what they look like. David Link has generated an [archive of simulated letters](http://www.alpha60.de/art/love_letters/archive/muc/).
Here are four examples:
```
HONEY MOPPET
YOU ARE MY AMOROUS RAPTURE.: MY KEEN LOVE. MY COVETOUS
INFATUATION THIRSTS FOR YOUR FERVENT LIKING. YOU ARE MY
CRAVING LIKING. MY CURIOUS RAPTURE FONDLY THIRSTS FOR YOUR
ANXIOUS ENCHANTMENT.
YOURS COVETOUSLY
MUC
```
```
DEAR DARLING
MY ADORABLE FELLOW FEELING WANTS YOUR SWEET INFATUATION
. MY ADORABLE ARDOUR LOVINGLY WISHES YOUR FERVOUR. MY
UNSATISFIED FELLOW FEELING EAGERLY HUNGERS FOR YOUR IMPATIENT
DESIRE. YOU ARE MY LOVING FERVOUR. MY WINNING WISH YEARNS FOR YOUR
KEEN HEART.
YOURS FERVENTLY
MUC
```
```
HONEY HONEY
MY BURNING FANCY EAGERLY TEMPTS YOUR INFATUATION.
YOU ARE MY FERVENT YEARNING. MY LIKING BEAUTIFULLY YEARNS FOR YOUR
LOVE. MY APPETITE TENDERLY CHERISHES YOUR BEAUTIFUL WISH.
YOU ARE MY EROTIC ADORATION.
YOURS SEDUCTIVELY
MUC
```
```
DARLING LOVE
YOU ARE MY AMOROUS ENCHANTMENT.: MY KEEN EAGERNESS.
YOU ARE MY DEVOTED ENCHANTMENT. MY YEARNING FERVENTLY LIKES
YOUR AMOROUS PASSION. MY EAGERNESS TENDERLY YEARNS FOR YOUR
1. A letter always starts with a certain opening {HONEY or DEAR or DARLING} + {MOPPET or DARLING or HONEY or LOVE}
2. the short form MY + ```adjective``` + ```noun``` + . always follows the other short form YOU ARE MY + ```adjective``` + ```noun``` + . This short form never follows the long form.
3. short form YOU ARE MY + ```adjective``` + ```noun``` + . is never followed by the same short form YOU ARE MY + ```adjective``` + ```noun``` + .
4. There are between 4 and 5 sentences (long or short form) per letter
5. A letter is always complementary closed with YOURS + ```adverb```
6. A letter is always signed by MUC (which means Manchester University Computer Department)
### Reconstituting Love Letters' algorithm
Now that we have an idea of the structure of Strachey's letters, let's get to work in formalising it's algorithmic generation.
Luckily, in terms of vocabulary, we can start with Monfort's arrays:
```javascript
// vocabulary
const first = ['DARLING', 'DEAR', 'HONEY', 'JEWEL'];
const second = ['DUCK', 'LOVE', 'MOPPET', 'SWEETHEART'];
This will certainly work. Yet I would like to have more variation with the sentences the function returns. At the moment it will always follow the long form structure.
How about we make ```adjective``` and ```adverb``` non mandatory, meaning they maybe there or maybe not. What I have in mind is something like:
```javascript
// function that randomly generates a long form sentence
To decide if an element (```adjective``` or ```adverb```) should be part of the sentence or not we need to write a ```maybe``` function which randomly chooses true or false if the element should be chosen from a given array.
Now we have most of the ingredients in place with the functions ```choice```, ```maybe```, ```long```, ```short```, ```opening```, ```closing``` right?
How about sequencing ```long``` and ```short``` forms? Remember what we observed above (i.e. a letter is 4 to 5 sentences long, short form MY + ```adjective``` + ```noun``` + . always follows the other short form, etc.)?
Following these observation here is a skeleton of an algorithm doing just that:
// did we just wrote the short form with "YOU ARE MY"
let YOU_ARE = false;
// write 5 sentences
for(let i = 0; i <5;i++){
// choose if the next sentence is a short or long
let sentence_form = choice(['short', 'long']);
if(sentence_form == 'short') {
// there's two types of short to switch from
if(YOU_ARE) {
// the ": MY" can only follow a "YOU ARE MY"
text += ": MY " + short();
YOU_ARE = false;
} else {
text += "YOUR ARE MY " + short();
// the ": MY" can only follow a "YOU ARE MY"
YOU_ARE = true;
}
} else if(sentence_form == 'long') {
text += long();
// make sure the next sentence is not ": MY"
YOU_ARE = false;
}
}
text += "\n"; // this is a new line!
// write the letter's closing
text += " " + closing();
text += "\n"; // this is a new line!
// sign
text += "MUC";
// return the letter's text
return text;
}
```
This function follows the algorithm we wrote above and adds the opening and closing sequence.
If you followed me during the session you should have a sketch looking more or less like the following, comprising all the functions we wrote. Here the letter is printed when ```x```is typed.