forked from bakoliasn/basic-javascript-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcowsay.js
More file actions
83 lines (75 loc) · 4.03 KB
/
Copy pathcowsay.js
File metadata and controls
83 lines (75 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*The Cow Whisperer
------------------------------------------
Challenge: create a file called cowsay.js in your Cloud9 project. Running this file should output a cow saying a random fortune. This is reproducing the cowsay command-line app. Make sure to add/commit this file and push it to your GitHub. If the fortune is longer than 30 characters, you have to wrap it on a new line. For extra brownie points, wrap your fortune at the word level instead of the letter level. */
function cowSay(randQuote) {
// Split random quote into strings of less than 30 characters, wrapping at the word level
var quoteArray = randQuote.split(" "); // split quote sting in to an array of words
var chunk = ""; // declare variable to hold the growing string of < 30 characters
var quoteSentenceArray = []; // declare array to push new ~30 char chunk strings to
var elseLastRun = false;
quoteArray.forEach(function(element) { // iterate through word array
if(chunk.length + 1 + element.length <= 30) { // check if adding the next sting plus a space to the current chunk will make its length > 30 chars
chunk = chunk + " " + element; // if the resulting string will be < 30 chars, add the current string to chunk
elseLastRun = false;
//console.log(chunk); // for debugging
}
else {
quoteSentenceArray.push(chunk); // if the resulting string will be > 30 chars, push the current chunk to the new array and set chunk to the current string
chunk = element;
elseLastRun = true;
//console.log(chunk); // for debugging
}
});
quoteSentenceArray.push(chunk);
//return quoteSentenceArray.join("\n");
// Put split quote into speech bubble
var asciiQuoteStart = " ________________________________";
var asciiQuoteEnd = " -------------------------------- ";
var asciiBubble = [];
var bubble = "";
for (var i = 0; i < quoteSentenceArray.length; i++) {
while (quoteSentenceArray[i].length < 31) { // pad all strings
quoteSentenceArray[i] = quoteSentenceArray[i] + " ";
}
if (i === 0) { // first line
quoteSentenceArray[i] = "/ " + quoteSentenceArray[i] + "\\";
}
else if (i === quoteSentenceArray.length - 1) { //last line
quoteSentenceArray[i] = "\\ " + quoteSentenceArray[i] + "/";
}
else {
quoteSentenceArray[i] = "| " + quoteSentenceArray[i] + "|";
}
asciiBubble.push(quoteSentenceArray[i]);
}
if (quoteSentenceArray.length === 1) {
bubble = asciiQuoteStart + "\n< " + quoteSentenceArray[0] + " >" + "\n" + asciiQuoteEnd + "\n";
}
else {
bubble = asciiQuoteStart + "\n" + asciiBubble.join("\n") + "\n" + asciiQuoteEnd + "\n";
}
// Create ascii cow
var asciiCow =[" \\ ^__^",
" \\ (oo)\\_______",
" (__)\\ )\\/\\ ",
" ||----w |",
" || ||"
];
return bubble + asciiCow.join("\n");
}
function randomQuote(randNum) { // Generate random quote
var quotes = [
"Happiness is a method of life, not a destination",
"Dont't cry because it's over, smile because it happened",
"Be yourself, everyone else is already taken",
"You only live once, but if you do it right, once is enough",
"Insanity is doing the same thing, over and over again, but expecting different results",
"Life is what happens to you while you're busy making other plans",
"I have not failed. I've just found 10,000 ways that won't work",
"I solemnly swear that I am up to no good",
"For every minute you are angry you lose sixty seconds of happiness",
"If you can't explain it to a six year old, you don't understand it yourself"
];
return quotes[randNum];
}
console.log(cowSay(randomQuote(Math.floor(Math.random() * 10)))); // Pass output of randomQuote to cowSay function