JavaScriptmancy - Basics - ES6 Destructuring - Exercises

ES6 Destructuring - 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

// 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 for C# Devs
Chapter 09 - Upgrading Your Everyday JavaScript Magic With ES6: Destructuring - Exercises

*/

/* EXERCISES */
narrate("=== 1. You Have One Try Left! ===");
narrate("Use ES6 Destructuring to find out what gort has in his back pocket");

/*
let gort = {
  name: 'Gort',
  title: 'the android', 
  backPocket: 'cucumber'
};
*/
gort.backPocket = 'cucumber';

let {backPocket} = gort;
mooleen.says(`It's a ${backPocket}!!`);
// => Mooleen says: It's a cucumber!!
gort.says('Correct!');
gort.says('What is at the end of this corridor? Turn north? Turn east?');

narrate("=== 2. What is at The End of This Corridor? ===");
narrate("Use ES6 Destructuring to find out what lies beyond at the end of the corridor, then to the north and then to the east. Save the destination in a variable called destiny");

let map = {
  startOfTheCorridor: 'an infinite wall of blackness',
  endOfTheCorridor: {
    north: {
        east: 'A white house, with a boarded front door'
        }
    }
};

let { endOfTheCorridor: { north: { east:destiny }}} = map;
mooleen.says(`It's ${destiny}!!`);
// => It's A white house, with a boarded front door!!

gort.says('Excellent! You are on a streak!');
gort.says('For the last test in this tutorial');
gort.says('What is the name of the second book in this bookshelf?')

/* A bookshelf materializes from thin air */

narrate("=== 3. The Second Book in The BookShelf ===");
narrate("Use ES6 Destructuring to find out which is the second book in the bookshelf:");

let bookshelf = [
  'Dungeons and Dragons 2nd Ed',
  'The Blade Itself',
  'The Silmarillion',
  'Mistborn',
  'The Wheel of Time'];

let [,book,] = bookshelf;
mooleen.says(`The name of the book is ${book}!`);
// => The name of the book is The Blade Itself!

gort.says('Congratulations! You have completed...