—Miguel de Cervantes ("Don Quixote" 190)
It's dark and Humphrey Chimpden Earwicker is still awake.
- Create a JavaScript function to help Humphrey fall asleep.
- Create an object that is congruent with the assumptions made by your function.
- Call your function, passing it your object. Repeat ad nauseam.
Humphrey likes to watch random YouTube videos. It doesn't take long before he finds one that puts him to sleep. Your function should open a pseudorandom YouTube video in a browser window. The web page opened should be referenced by the value of a property of the first argument passed to the function. Let the function assume that the number of arguments passed to the function is always greater than zero; the first argument passed to the function is always an object; said object always has two or more properties; the value of each property of said object is always a string that is always a URL; and said URL always refers to a YouTube web page.
/* 0 - Goats */ function output(message, mute) { console.log(message); if (mute !== true) { alert(message); } } function dialogue(speaker, message, mute) { output(speaker + ': ' + message, mute); } function quietDialogue(speaker, message) { output(speaker + '(quietly): ' + message, true); } function voiceOver(message, mute) { output('[' + message + ']', mute); } function Goat() { } Goat.prototype = { toString: function() { return 'goat'; } } function Protagonist(name, numberNeededToSleep) { this.name = name; this.numberNeededToSleep = numberNeededToSleep; this.currentCount = 0; //voiceOver(name + ' exists.'); } Protagonist.prototype = { isAwake: function() { return this.currentCount < this.numberNeededToSleep; }, wakeUp: function() { if (!this.isAwake()) { voiceOver(this.name + "'s dream is over."); } this.currentCount = 0; }, count: function(thing) { if (arguments.length === 0) { voiceOver(this.name + " lost count."); this.wakeUp(); } else { this.currentCount++; } }, toString: function() { return this.name; } }
/* 1 - Goats */ function goToSleep(patient) { var D = 'fisherman'; var P = patient.name; voiceOver(P + " is having trouble sleeping."); dialogue(D, 'Trouble sleeping ' + patient.name + '?'); if (patient.isAwake()) { dialogue(P, 'You have no idea.'); } else { voiceOver(P + ' is dreaming.'); dialogue(D, 'I said... TROUBLE SLEEPING?'); patient.wakeUp(); dialogue(P, 'Indeed.'); } dialogue(D, 'I can help.') dialogue(D, 'Count my goats as I ferry them across the river.') var goat = new Goat(); while (patient.isAwake()) { var n = patient.currentCount; if (n + 1 === patient.numberNeededToSleep) { voiceOver(P + ' is getting sleepy.'); } patient.count(goat); n = patient.currentCount; var s = n===0 || n>1 ? 's' : ''; quietDialogue(P, n + ' ' + goat.toString() + s); } dialogue(D, 'Sweet dreams ' + P + '.'); patient.count(); // Nooooo... Don't do it Humphrey! }
/* 2 - Goats */ // Number of things HCE needs to count to fall asleep. var numThings = 42; var HCE = new Protagonist('Humphrey', numThings);
/* 3 - Goats */ goToSleep(HCE);
/* 0 - YouTube */ var recentValues = []; function randomElement(someArray) { // Interesting Experiment: use Math.trunc instead var randomIndex = (Math.random() * someArray.length) | 0; return someArray[randomIndex]; } function randomValue(someObject) { var arrayOfKeys = Object.keys(someObject); var randomKey; do { randomKey = randomElement(arrayOfKeys); } while (recentValues.indexOf(randomKey) > -1); recentValues.push(randomKey); if (recentValues.length > arrayOfKeys.length-3) { recentValues.splice(0,1); } return someObject[randomKey]; }
/* 1 - YouTube */ function openRandomWebPage(objectOfUrls) { var randomUrl = randomValue(objectOfUrls); window.open(randomUrl); }
/* 2 - YouTube */ var webPageUrls = { MySisterMyDaughter: 'https://www.youtube.com/watch?v=wnrdetFAo1o', YouCantHandleTheTruth: 'https://www.youtube.com/watch?v=9FnO3igOkOk', AllQuestionsAnswered: 'https://www.youtube.com/watch?v=CDokMxVtB3k', AdviceToYoungPeople: 'https://www.youtube.com/watch?v=75Ju0eM5T2c', ExpressYourself: 'https://www.youtube.com/watch?v=AHv0md_HCYc', DoItLikeYouDo: 'https://www.youtube.com/watch?v=Tyt_4N4LNz4', MakeItFunky: 'https://www.youtube.com/watch?v=3_0alsFnxwI', ItsAMansWorld: 'https://www.youtube.com/watch?v=EBLNYuKLYD0', 433: 'https://www.youtube.com/watch?v=JTEFKFiXSx4', WayOfTheInterceptingFist: 'https://www.youtube.com/watch?v=2qvYa5t-JUc' }
/* 3 - YouTube */ openRandomWebPage(webPageUrls);
/* Magic Functions */ function elementIsHidden(id) { return document.getElementById(id).style.display === 'none'; } function elementIsShowing(id) { return !elementIsHidden(id); } function showElement(id) { document.getElementById(id).style.display = 'inline'; } function hideElement(id) { document.getElementById(id).style.display = 'none'; } function toggleElement(id) { if (elementIsHidden(id)) showElement(id); else hideElement(id); } function toggleStep(n) { toggleElement('step' + n + getOption()); hideMagic(); } function hideStep(n) { hideElement('step' + n + getOption()); } function showStep(n) { showElement('step' + n + getOption()); } function hideScript() { for (var n=0; n<=3; n++) hideStep(n); hideMagic(); } function showScript() { for (var n=0; n<=3; n++) showStep(n); hideMagic(); } function runScript() { if (inTheDark) { if (failedToAskFirst === null) { alert("That's not the rock I want. Bring me another one."); failedToAskFirst = true; } else { alert("Seriously. That's not the rock I want."); alert("Tip: Ask if you're not sure."); } return; } hideMagic(); if (getOption() === GoatsOption) { goToSleep(HCE); } else { openRandomWebPage(webPageUrls); } showMagicButton(); } function getOption() { if (elementIsHidden('hiddenRequirements')) { return GoatsOption; } else { return YouTubeOption; } } function question() { if (inTheDark) { if (failedToAskFirst === null) { //alert("You didn't get burned by a game of " + '"Bring me another rock".'); } else if (failedToAskFirst){ alert('Watch out! "Bring me another rock" is real.'); } inTheDark = false; alert("Good! Now have fun."); } hideScript(); toggleElement('hiddenRequirements'); hideMagic(); } function hideMagicButton() { hideElement('magicButton'); } function showMagicButton() { showElement('magicButton'); } function hideMagic() { hideElement('MagicFunctions'); hideMagicButton(); } function toggleMagicTrick() { if (elementIsShowing('MagicFunctions')) { hideMagic(); } else { showElement('MagicFunctions'); } } var GoatsOption = 'Goats'; var YouTubeOption = 'YouTube'; var inTheDark = true; var failedToAskFirst = null;