JavaScriptmancy - Basics - JavaScript Strings - Exercises
JavaScript 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);
}
JavaScript
// 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 08 - A Guide to Strings, Finding the Right Words and Proper Spell Intonation - Exercises
*/
/* EXERCISES */
narrate("=== Word Barrage! ===");
narrate("Create a multiline string (a string that spans several lines) in three different ways. ");
let magicWords1 = "Roses are grey, " +
"Violets are grey, " +
"I'm a dog";
let magicWords2 = "Roses are grey,\
Violets are grey,\
I'm a dog";
// this keeps whitespace
let magicWords3 = `Roses are grey,
Violets are grey,
I'm a dog`;
narrate("=== Rack Your Brains! ===");
narrate("Mooleen has no idea what the magic words could be so she starts thinking, and reflecting, racking her brains for a long, long time. Write a function that fulfills the following snippet using ES5:");
/*
rackBrains(12);
// => mooleen racks her brains for 12 long minutes
*/
function rackBrains(minutes){
console.log('Mooleen racks her brain for ' + minutes + ' long minutes');
}
rackBrains(12);
// => Mooleen racks her brain for 12 long minutes
narrate("=== Rack Your Brains With ES6! ===");
narrate("Update the `rackBrains` function to use ES6 template literals.");
function rackBrainsES6(minutes){
console.log(`Mooleen racks her brain for ${minutes} long minutes`);
}
rackBrainsES6(12);
// => Mooleen racks her brain for 12 long minutes
narrate("=== ### Create The Moolen DSL with Tags! ===");
narrate("Use tags to create a Mooleen Domain Specific Language. It should work like this:");
/*
console.log(rackBrains`for 15 minutes`);
// => mooleen racks her brains for 12 long minutes
console.log(says`what is going on?`);
// => mooleen says: "What is going on?"
*/
function rackBrains(literals, ...substitutions){
let phrase = literals[0];
substitutions.forEach(function(s, idx){
phrase += `${s}${literals[idx+1]}`
});
return...