28 lines
834 B
JavaScript
Raw Permalink Normal View History

2023-01-18 16:18:38 +01:00
// Sketch 2.2: Array + Index + Randomness!
var NAMES = ["David", "Karin", "Sigrid", "Nanna", "Laura", "Maaike"];
var ACTIVITIES = ["piano", "tennis", "chess", "records"];
var ADJ = ["blue", "yellow", "black", "orange", "fabulous", "expensive", "ridiculous"];
var CLOTHES = ["jacket", "sweater", "cap", "scarf"];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
}
// function selecting a random element from an array
function choice(array_to_choose_from) {
let random_index = floor(random(array_to_choose_from.length));
return array_to_choose_from[random_index];
}
function keyTyped() {
if(key == 'x') {
print(choice(NAMES) + " wears a " + choice(ADJ) + " " + choice(CLOTHES) + " when " + choice(NAMES) + " plays " + choice(ACTIVITIES));
}
}
// Reference: https://p5js.org/reference/