week2 examples

This commit is contained in:
gauthiier 2016-11-11 12:55:14 +01:00
parent e592c370d8
commit 95e96e1230
6 changed files with 322 additions and 0 deletions

View File

@ -0,0 +1,75 @@
// bookstore.js
/*
var book1_title = "Ways of Curating";
var book1_price = 16.95;
var book1_author = "Hans Ulrich Orbist";
var book2_title = "Ardor";
var book2_price = 39.5;
var book2_author = "Roberto Calasso";
var book3_title = "Why Grow Up?";
var book3_price = 15.95;
var book3_author = "Susan Neiman";
*/
var book1 = {
'title': "Ways of Curating",
'price': 16.95,
'author': "Hans Ulrich Orbist"
};
var book2 = {
'title': "Ardor",
'price': 39.5,
'author': "Roberto Calasso"
};
var book3 = {
'title': "Why Grow Up?",
'price': 15.95,
'author': "Susan Neiman"
};
var array_of_books = [
{ 'title': "Ways of Curating",
'price': 16.95,
'author': "Hans Ulrich Orbist"
},
{ 'title': "Ardor",
'price': 39.5,
'author': "Roberto Calasso"
},
{
'title': "Why Grow Up?",
'price': 15.95,
'author': "Susan Neiman"
}
];
function print_the_book(a_book) {
console.log('--------------');
console.log('Title: ' + a_book.title);
console.log('Price: ' + a_book.price);
console.log('Author: ' + a_book.author);
}
for(var i = 0; i < array_of_books.length; i++) {
print_the_book(array_of_books[i]);
}
// console.log(book1);
// console.log(book2);
// console.log(book3);
//console.log(book3.author);
//console.log(book3_author);

View File

@ -0,0 +1,11 @@
{
"name": "bookstore",
"version": "1.0.0",
"description": "test code for gr01 bookstore",
"main": "bookstore.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "gauthier",
"license": "ISC"
}

View File

@ -0,0 +1,94 @@
/*
file: letter.js
desc: simple script that generates a (skeleton) letter
based on Christopher Stratchey's Love Letter as
analysed by Noah Wardrip-Fruin in "Digital Media Archeology"
author: gauthier
date: 11/11/16
*/
// import chance (http://chancejs.com)
var chance = require('chance').Chance(); // npm install --save chance
// import word-wrap (https://www.npmjs.com/package/word-wrap)
var wrap = require('word-wrap'); // npm install --save word-wrap
// Stratchey's Love letter vocabulary database
// based on Noah Wardrip-Fruin's "Digital Media Archeology" Table 14.1
const first = ['DARLING', 'DEAR', 'HONEY', 'JEWEL'];
const second = ['DUCK', 'LOVE', 'MOPPET', 'SWEETHEART'];
const adjectives = ['ADORABLE', 'AFFECTIONATE', 'AMOROUS', 'ANXIOUS', 'ARDENT', 'AVID', 'BREATHLESS', 'BURNING', 'COVETOUS', 'CRAVING', 'CURIOUS', 'DARLING', 'DEAR', 'DEVOTED', 'EAGER', 'EROTIC', 'FERVENT', 'FOND', 'IMPATIENT', 'KEEN', 'LITTLE', 'LOVEABLE', 'LOVESICK', 'LOVING', 'PASSIONATE', 'PRECIOUS', 'SWEET', 'SYMPATHETIC', 'TENDER', 'UNSATISFIED', 'WISTFUL'];
const nouns = ['ADORATION', 'AFFECTION', 'AMBITION', 'APPETITE', 'ARDOUR', 'CHARM', 'DESIRE', 'DEVOTION', 'EAGERNESS', 'ENCHANTMENT', 'ENTHUSIASM', 'FANCY', 'FELLOW FEELING', 'FERVOUR', 'FONDNESS', 'HEART', 'HUNGER', 'INFATUATION', 'LIKING', 'LONGING', 'LOVE', 'LUST', 'PASSION', 'RAPTURE', 'SYMPATHY', 'TENDERNESS', 'THIRST', 'WISH', 'YEARNING'];
const adverbs = ['AFFECTIONATELY', 'ANXIOUSLY', 'ARDENTLY', 'AVIDLY', 'BEAUTIFULLY', 'BREATHLESSLY', 'BURNINGLY', 'COVETOUSLY', 'CURIOUSLY', 'DEVOTEDLY', 'EAGERLY', 'FERVENTLY', 'FONDLY', 'IMPATIENTLY', 'KEENLY', 'LOVINGLY', 'PASSIONATELY', 'SEDUCTIVELY', 'TENDERLY', 'WINNINGLY', 'WISTFULLY'];
const verbs = ['ADORES', 'ATTRACTS', 'CARES FOR', 'CHERISHES', 'CLINGS TO', 'DESIRES','HOLDS DEAR', 'HOPES FOR', 'HUNGERS FOR', 'IS WEDDED TO', 'LIKES', 'LONGS FOR', 'LOVES', 'LUSTS AFTER', 'PANTS FOR', 'PINES FOR', 'PRIZES', 'SIGHS FOR', 'TEMPTS', 'THIRSTS FOR', 'TREASURES', 'WANTS', 'WISHES', 'WOOS', 'YEARNS FOR'];
/**
* Picks a random element from an array
* @param {Array} array
* @return {Object} choice
*/
function choice(array) {
var index = chance.natural({'min': 0, 'max': array.length - 1});
return array[index];
}
/**
* Randomly picks or not a random element from an array
* @param {Array} array
* @return {Object} choice
* @return {String} empty string
*/
function maybe(array) {
if( chance.bool() ) {
return choice(array);
} else {
return '';
}
}
/**
* Generates a short phrase consisting of a randomly chosen
* adjective and noun
* @return {String} phrase
*/
function short() {
return choice(adjectives) + ' ' + choice(nouns);
}
/**
* Generates a sentence composed of randomly chosen
* adjective, noun, adverb, verb, adjective and noun
* @return {String} sentence
*/
function long() {
return 'MY ' + maybe(adjectives) + ' ' + choice(nouns) + ' ' +
maybe(adverbs) + ' ' + choice(verbs) + ' ' +
'YOUR ' + maybe(adjectives) + ' ' + choice(nouns) + '. ';
}
// format the output (header)
console.log('\n\n\n\n'); // 4 x newline ('\n')
// in order to use word-wrap, I need to construct a single string
// (text) containing all generated sentences
var text = '';
// loop generating 5 sentences
for(var i = 0; i < 5; i++) {
// concatenate the new sentence generated by long() with the text string
text += long(); // (+=) text = text + long()
}
// wrap the text with a width of 65 characters and log (output) it to the console
console.log(wrap(text, {'width': 65}));
// format for the output (footer)
console.log('\n\n\n\n'); // 4 x newline ('\n')

View File

@ -0,0 +1,16 @@
{
"name": "letter",
"version": "1.0.0",
"description": "letter generation example gr01",
"main": "letter.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "gauthier",
"license": "ISC",
"dependencies": {
"chance": "^1.0.4",
"commander": "^2.9.0",
"word-wrap": "^1.1.0"
}
}

View File

@ -0,0 +1,110 @@
/*
file: letter.js
desc: simple script that generates a (skeleton) letter
based on Christopher Stratchey's Love Letter as
analysed by Noah Wardrip-Fruin in "Digital Media Archeology"
author: gauthier
date: 11/11/16
*/
// import chance (http://chancejs.com)
var chance = require('chance').Chance(); // npm install --save chance
// import word-wrap (https://www.npmjs.com/package/word-wrap)
var wrap = require('word-wrap'); // npm install --save word-wrap
// Stratchey's Love letter vocabulary database
// based on Noah Wardrip-Fruin's "Digital Media Archeology" Table 14.1
const first = ['DARLING', 'DEAR', 'HONEY', 'JEWEL'];
const second = ['DUCK', 'LOVE', 'MOPPET', 'SWEETHEART'];
const adjectives = ['ADORABLE', 'AFFECTIONATE', 'AMOROUS', 'ANXIOUS', 'ARDENT', 'AVID', 'BREATHLESS', 'BURNING', 'COVETOUS', 'CRAVING', 'CURIOUS', 'DARLING', 'DEAR', 'DEVOTED', 'EAGER', 'EROTIC', 'FERVENT', 'FOND', 'IMPATIENT', 'KEEN', 'LITTLE', 'LOVEABLE', 'LOVESICK', 'LOVING', 'PASSIONATE', 'PRECIOUS', 'SWEET', 'SYMPATHETIC', 'TENDER', 'UNSATISFIED', 'WISTFUL'];
const nouns = ['ADORATION', 'AFFECTION', 'AMBITION', 'APPETITE', 'ARDOUR', 'CHARM', 'DESIRE', 'DEVOTION', 'EAGERNESS', 'ENCHANTMENT', 'ENTHUSIASM', 'FANCY', 'FELLOW FEELING', 'FERVOUR', 'FONDNESS', 'HEART', 'HUNGER', 'INFATUATION', 'LIKING', 'LONGING', 'LOVE', 'LUST', 'PASSION', 'RAPTURE', 'SYMPATHY', 'TENDERNESS', 'THIRST', 'WISH', 'YEARNING'];
const adverbs = ['AFFECTIONATELY', 'ANXIOUSLY', 'ARDENTLY', 'AVIDLY', 'BEAUTIFULLY', 'BREATHLESSLY', 'BURNINGLY', 'COVETOUSLY', 'CURIOUSLY', 'DEVOTEDLY', 'EAGERLY', 'FERVENTLY', 'FONDLY', 'IMPATIENTLY', 'KEENLY', 'LOVINGLY', 'PASSIONATELY', 'SEDUCTIVELY', 'TENDERLY', 'WINNINGLY', 'WISTFULLY'];
const verbs = ['ADORES', 'ATTRACTS', 'CARES FOR', 'CHERISHES', 'CLINGS TO', 'DESIRES','HOLDS DEAR', 'HOPES FOR', 'HUNGERS FOR', 'IS WEDDED TO', 'LIKES', 'LONGS FOR', 'LOVES', 'LUSTS AFTER', 'PANTS FOR', 'PINES FOR', 'PRIZES', 'SIGHS FOR', 'TEMPTS', 'THIRSTS FOR', 'TREASURES', 'WANTS', 'WISHES', 'WOOS', 'YEARNS FOR'];
/**
* Picks a random element from an array
* @param {Array} array
* @return {Object} choice
*/
function choice(array) {
var index = chance.natural({'min': 0, 'max': array.length - 1}); // **** NOTE: 'max': array.length - 1
return array[index];
}
/**
* Randomly picks or not a random element from an array
* @param {Array} array
* @return {Object} choice
* @return {String} empty string
*/
function maybe(array) {
if( chance.bool() ) {
return choice(array);
} else {
return '';
}
}
/**
* Generates a short phrase consisting of a randomly chosen
* adjective and noun
* @return {String} phrase
*/
function short() {
return choice(adjectives) + ' ' + choice(nouns);
}
/**
* Generates a sentence composed of randomly chosen
* adjective, noun, adverb, verb, adjective and noun
* @return {String} sentence
*/
function long() {
return 'MY ' + maybe(adjectives) + ' ' + choice(nouns) + ' ' +
maybe(adverbs) + ' ' + choice(verbs) + ' ' +
'YOUR ' + maybe(adjectives) + ' ' + choice(nouns) + '. ';
}
// format the output (header)
console.log('\n\n\n\n'); // 4 x newline ('\n')
/*
Group 02: During the class I did not have time to add the word-wrapping
to the example we coded in class. It is here used to format the output of the script.
What is different from what you may have, is that I construct a single string called 'text'
which holds all generated sentences in the loop below. Thus, in the loop, rather than
outputting the sentence (console.log(long())), I simply concatenate the new sentence with
'text' (text += long()), which, in turn, holds all 5 sentences. The I use word-wrap to
format the 'text' depending on the width (in characters) I want the resulting letter to
be (here 65 characters).
for more information https://www.npmjs.com/package/word-wrap
*/
// in order to use word-wrap, I need to construct a single string
// (text) containing all generated sentences
var text = '';
// loop generating 5 sentences
for(var i = 0; i < 5; i++) {
// concatenate the new sentence generated by long() with the text string
text += long(); // (+=) text = text + long()
}
// wrap the text with a width of 65 characters and log (output) it to the console
console.log(wrap(text, {'width': 65}));
// format for the output (footer)
console.log('\n\n\n\n'); // 4 x newline ('\n')

View File

@ -0,0 +1,16 @@
{
"name": "letter",
"version": "1.0.0",
"description": "generated letters",
"main": "letter.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "gauthier",
"license": "ISC",
"dependencies": {
"chance": "^1.0.4",
"commander": "^2.9.0",
"word-wrap": "^1.1.0"
}
}