xxxxxxxxxx
78
//1. Delcare a variable words and intialize it with an empty erray
let words = []; //creating an empty array 'words'
//2. Declare a variable p to hold paragraph element
let p; //naming a variable P
//3. Declare string variable and initialize with an empty string
let str = ''; //creating an empty string
//4. pre-load text file and call process when it's done
function preload() { //preloading function
//load the text file and after it's completed run process
loadStrings('bang.txt', process);
}
function setup() {
//15. don't draw canvas
noCanvas(); //no background
//16. create a paragraph element and assign it to p
p = createP(); //creating paragraph element
}
function draw() {
//17. every 30 frames (0.5s)
if(frameCount%30 ==0) { //every 0.5s,
//pick a random word out of the words array and assign it to next_word
let next_word = random(words); //select a random word
//add next word and space to str variable
str += next_word + ' '; //add next word into string?
//update the html element with the entire text
p.html(str); //print the updated string
}
}
//5. turning text into individual words
function process(lines) {
console.log
//5. iterate through each line of the text
for(let ln of lines) { //run through each line in the text
//6. split into words units and store as an array
let tokens = splitTokens(ln); //
//7. Add token into the words array as indivudal words
words = words.concat(tokens); //
}
//8. Loop through words backwards;
for(let w = words.length - 1; w >=0; w--) {
//9. Get words stored at index w, assigned it to a variable called word
let word = words[w];
//10. replace punctuations with nothing and assign it to word
word = word.replace(/[\-_!?.,;:\(\)]/g, '');
//11. take word, lower case, and assign it to word
word = word.toLowerCase();
//12. take word, get rid of whitespace before/after and assign it back to word
word = word.trim();
//13. if empty word, remove it from the array
if(word.length < 1) words.splice(w, 1);
//14. otherwise assign back to the array at index w
else words[w] = word;
}
}