Skip to content

Instantly share code, notes, and snippets.

@Anna-Myzukina
Last active September 21, 2018 06:19
Show Gist options
  • Select an option

  • Save Anna-Myzukina/d95fafeb9c6fb97f2532b1016bd84761 to your computer and use it in GitHub Desktop.

Select an option

Save Anna-Myzukina/d95fafeb9c6fb97f2532b1016bd84761 to your computer and use it in GitHub Desktop.
count-to-6
//Exercise 1
/* write a program.js that outputs the
string "HELLO ES6" to the console
*/
console.log("HELLO ES6");
//Exercise 10
/*You will be given two arguments to your program: a username, and a comment. Both will potentially contain HTML-unsafe characters (namely ', ", <, >, and &). Your job is to use tagged template strings to log a safely-escaped HTML rendering of the comment.
For example, if the username is "domenic" and the comment is "<dl> is a fun tag", you should output:
<b>domenic says</b>: "&lt;dl&gt; is a fun tag"
As before, you could start doing this using simple ES5 constructs. But the goal is for you to write a function that you can use as a tag in a tagged template string, that will do any escaping automatically.
If you don't remember the corresponding escape sequences, they are:
' | &apos;
" | &quot;
< | &lt;
> | &gt;
& | &amp;
*/
console.log(html`<b>${process.argv[2]} says</b>: "${process.argv[3]}"`);
function html(pieces, ...substitutions) {
var result = pieces[0];
for (var i = 0; i < substitutions.length; ++i) {
result += escape(substitutions[i]) + pieces[i + 1];
}
return result;
}
function escape(s) {
return s.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/'/g, "&apos;")
.replace(/"/g, "&quot;");
}
//Exercise 2
/*fWrite a generator function factorial that, given an input number, starts at 1 and goes
up to the number, yielding the factorial of each number along the way.
Don't use recursion. Use a loop.
Follow this boilerplate:
*/
function *factorial (n) {
var result = 1;
for (var i = 1; i <= n; i++) {
result *= i;
yield result;
}
}
for (var n of factorial(5)) {
console.log(n)
}
//Exercise 2
/*You will be given a name as the first argument to your program (process.argv[2]).
You should output a two-line message, first greeting that person, and then telling them their name in lowercase. */
console.log(`Hello, ${process.argv[2]}!
Your name lowercased is "${process.argv[2].toLowerCase()}".`);
//Exercise 3
/*You'll be given a variable number of arguments (process.argv[2] onward) to your program, all of which will be strings. Use arrow functions to perform a map-reduce operation over them, before outputting them to the console. That is, your solution should start with something like:
var inputs = process.argv.slice(2);
var result = inputs.map(// what goes here? )
.reduce( //what goes here? );
In particular, you should:
* Map all incoming strings to their first character
* Reduce the result by concatenating them together
So an input of ["Hello", "arrow", "functions"] would become "Haf".
To show your work, you should output the transformation to the console. The above example would be:
[Hello,arrow,functions] becomes "Haf"
*/
var inputs = process.argv.slice(2);
var result = inputs.map(s => s[0])
.reduce((soFar, s) => soFar + s);
console.log(`[${inputs}] becomes "${result}"`);
//Exercise 4
/*Let's do exactly that. Starting with the original code example above,
replace the anonymous function passed to setImmediate with an arrow function
*/
var foot = {
kick: function () {
this.yelp = "Ouch!";
setImmediate(() => console.log(this.yelp));
}
};
foot.kick();
//Exercise 5
/*Let's assume that we need to extract certain pieces of information concerning a user from an input array, which has the following format:
[userId, username, email, age, firstName, lastName]
The consecutive values of this array will be given as execution parameters, so you can obtain them using:
let userArray = process.argv.slice(2); // userArray equals here e.g. [1,
"jdoe", "[email protected]", "John", "Doe"]
Your task is to extract information about the username and email to a separate object containing two properties - username and email - and log it out.
let userArray = process.argv.slice(2);
// what goes here?
console.log(// your result ); // {username: "jdoe", email: "[email protected]"}
*/
let args = process.argv.slice(2);
let result = {};
[, result.username, result.email] = args;
console.log(result);
//Exercise 6
/*You'll be given a variable number of arguments (process.argv[2] onward) to your program, all of which will be numbers.
Use the ES6 spread operator to find the minimum value of these numbers and log it to the console.
And let's log the list of numbers themselves, to make it clearer. So the output should be, for example:
The minimum of [18,5,7,24] is 5
*/
var numbers = process.argv.slice(2);
var min = Math.min(...numbers);
console.log(`The minimum of [${numbers}] is ${min}`);
//Exercise 7
/*This exercise uses a slightly different format than usual. This time, your goal is to write a Node module whose default export is an average function. You don't need to print anything to the console. That is, your solution should look something like:
module.exports = function average(// what goes here ) {
// what goes here?
};
We'll test your module by passing it a few different sets of arguments, e.g.
average(1, 2, 3) and average(5, 10), and making sure that it works in all cases.
*/
module.exports = (...args) => {
var sum = args.reduce((soFar, value) => soFar + value, 0);
return sum / args.length;
};
//Exercise 8
/*write a Node module whose default export is a function. This time it will take two arguments: a lower
bound and an upper bound. Your function should return the midpoint between those two bounds.
However, your function should have sensible defaults. The lower bound should
default to 0, and the upper bound should default to 1.
As before, you don't need to print anything to the console. Your solution should look something like:
module.exports = function midpoint(// what goes here ) {
// what goes here?
};
We'll test your module by passing it a few different sets of arguments: sometimes none, sometimes one, sometimes two. Sometimes we'll pass in an explicit
undefined, and you should be sure to treat that as the default
*/
module.exports = (x = 0, y = 1) => (x + y) / 2;
//Exercise 9
/*For this exercise, you should write a Node module whose default export is a function that will make a string really important. It should do this by adding a bunch of exclamation marks after it. The exact number of exclamation marks should be configurable, but by default, it should be equal to the length of
the string. So:
makeImportant("Hi", 5); // => "Hi!!!!!"
makeImportant("Hi"); // => "Hi!!"
makeImportant("Hello?", undefined); // => "Hello?!!!!!!"
Bonus ES6 knowledge that might be helpful: ES6 includes a String.prototype.repeat that does exactly what you'd imagine.
*/
module.exports =
(string, bangs = string.length) => string + "!".repeat(bangs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment