JavaScriptmancy - Basics - Function Arbitrary Arguments - Exercises
Function Arbitrary Arguments - 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 04 - Useful Function Patterns: Arbitrary Arguments - Exercises
*/
randalf.says('He did it. He gained access to the REPL');
randalf.says('He understood its secret language and everything changed');
randalf.says(`
All of the sudden he gained the power to command the world around him.
No longer did he need to use his bare hands he could just create things out of nothingness.
Disturbing but amazing to witness.
And then he started teaching it, and a golden age of civilization began.
`);
randalf.says('and it all started with eggs...');
randalf.stomach.says('brrrrrrr....');
randalf.says('hmm... care for some eggs??');
// exercises
narrate("=== ### Boil Some Eggs! === ");
narrate("Transform this function to enable boiling an arbitrary number of eggs using and the arguments object.");
function boil(item){
console.log('You summon a bubble of boiling water that boils the ' + item);
}
mooleen.weaves("boil('egg')");
// => You summon a bubble of boiling water that boils the egg
// now boil all of these!
var dragonEgg = 'dragon egg',
egg = 'a unasumming egg',
goldenEgg = 'a golden egg';
// solution!
mooleen.says('I think I am getting the hang of it...');
function boilMany(){
var items = Array.prototype.slice.apply(arguments);
items.forEach(function(item) {
boil(item);
});
}
mooleen.weaves('boilMany(dragonEgg, egg, goldenEgg)');
// => You summon a bubble of boiling water that boils the dragon egg
// You summon a bubble of boiling water that boils the a unasumming egg
// You summon a bubble of boiling water that boils the a golden egg
narrate("=== ### Now Improve it With ES6 === ");
narrate("Use ES6 rest parameters to improve `boilMany`.");
mooleen.says('All right! I reckon that if I... ');
function boilManyES6(...items){
...