JavaScriptmancy - Basics - Objects - Exercises
Objects - 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 06 - The Basics Of Objects - Exercises
*/
narrate(`This must be the weirdest piece of dune man has ever known. There's two wizards surrounded by a critter, a mouse, a giant bat, a venomous frog, a monster, a teeny tiny and a giant spider, a stealthy monster, a crazy monkey, a dark spider, a sugary critter?, an ooze and a ptero... a pterodactyl whatever that may be.
`);
randalf.says("And that's how you summon creatures to your cause! An army!");
mooleen.says("Ahá");
mooleen.says("Summon them from where?");
randalf.says("hmm... good question!");
randalf.says("Powerful javascriptmancers can create stuff out of nothing");
randalf.says("Initiates summon creatures from..." +
"wherever creatures come from");
randalf.says("There's a lot of sand here... why not create a sand golem?");
// Exercises
narrate("### Create a Sand Golem! === ");
narrate("Use an object initializer to create a sand golem. It should satisfy the following snippet of code:");
/*
sandGolem.toString();
// (returns) => Giant Sand Golem
sandGolem.walksTo(1,1);
// => Giant Sand Golem walks to (1,1);
sandGolem.grabs('spider');
// => Giant Sand Golem grabs spider
sandGolem.grabs('monkey', 'venomous frog');
// => Giant Sand Golem grabs monkey and venomous frog
sandGolem.grabbedStuff;
// (returns) => ['spider', 'monkey', 'venomous frog']
*/
mooleen.concentrates();
narrate("A sudden wind appears from out of nowhere, a small whirlwind that sucks the sand beside mooleen and grows, and grows, and grows until it becomes and imposing giant figure that vaguely resembles something human.");
let sandGolem = {
position: {x: 0, y: 0},
walksTo(x, y){
console.log(this + ' walks to (' + x + ',' + y + ')');
this.position.x = x;
this.position.y = y;
},
toString(){
...