JavaScriptmancy - Basics - Function Overloading - Exercises
Function Overloading - Exercises
by vintharas
HTML
<pre><code id="theWorld">
</code></pre>
CSS
pre {
font-size: 0.9rem;
padding: 2%;
background: #333;
color: white;
}
pre .world {
display: block;
white-space: pre-wrap;
}
pre .world-error{
color: rgba(255,0,0,0.7);
}
pre .world-info {
color: grey;
font-style: italic;
padding: 10px 0;
}
pre .world-spell{
color: rgba(90,90,255,1);
}
Babel + JSX
'use strict'
// log stuff in the home-made console to the write in addition to
// logging it to the browser console
startWorld();
/*
JavaScript-mancy: Mastering the Arcane Art of Writing Awesome JavaScript
Chapter 05 - Useful Function Patterns: Function Overloading - Exercises
*/
randalf.says("haha! And that's what you need to known about overloading!");
mooleen.says("What am I doing here?");
randalf.says("Oh yeah that...");
randalf.says("You are the Chosen one!");
mooleen.says("Yes, yes, the chosen for what?");
randalf.says("You are going to fix everything! " +
"Bring balance to the force and all that");
randalf.says("But first you need to learn!");
randalf.says("Right now you wouldn't stand a chance");
mooleen.says("Well 'Great' wouldn't agree would he?");
randalf.says("Oh child, that was just an avatar");
randalf.says("Do you think that this paranoid psychotic megalomaniac " +
"would come to you in the flesh??");
// Exercises
narrate("=== ### Create Your Own Avatar === ");
narrate("Write a function `createAvatar` using function overloading by inspecting arguments that satisfies the following snippet:");
/*
createAvatar('a blue wisp hovering around');
// => you create an avatar in the form of a blue wisp hovering around
createAvatar({ appearance: 'a blue wisp', stance: 'hovering around'});
// => you create an avatar in the form of a blue wisp hovering around
*/
mooleen.says('An avatar...');
mooleen.says('Let me see if I can do it myself...');
function createAvatar(){
if (typeof arguments[0] === "string"){
var description = arguments[0];
console.log('you create an avatar in the form of ' + description);
} else {
var attributes = arguments[0],
appearance = attributes.appearance,
stance = attributes.stance;
console.log('you create an avatar in the form of ' + appearance + " " + stance);
}
}
mooleen.weaves("createAvatar('a blue wisp hovering around')");
// => you create an avatar in...