From 9cca21f57fa9248ddb013e56ca781b29feb32ac6 Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 25 Mar 2019 19:49:34 +0530 Subject: [PATCH] [Fix] Removed unnecessary IIFEs from ES6 challenges (#34538) * feat: removed IIFE and added solution * feat: updated challenges seed, test and solution * style: removed semicolon * feat: updated seed and solution * feat: updated challenges seed and solution * feat: updated test, seed and solution * fix: added seed code to solution * fix: removed function and added solution * fix: removed makeClass function and fixed solution * style: removed excessive semicolons * Fixed spacing for note in instructions section * fix: removed assert messages and used const * fix: regex fails correctly now --- ...t-parameters-for-your-functions.english.md | 18 +++--- ...o-define-a-constructor-function.english.md | 36 ++++-------- ...to-assign-variables-from-arrays.english.md | 24 +++----- ...bject-as-a-functions-parameters.english.md | 33 ++++++----- ...-to-control-access-to-an-object.english.md | 55 ++++++++----------- ...erator-with-function-parameters.english.md | 27 +++++---- ...tor-to-evaluate-arrays-in-place.english.md | 24 ++++---- 7 files changed, 91 insertions(+), 126 deletions(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.english.md index 79c53d3e1c..2f89f21f1f 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.english.md @@ -23,11 +23,11 @@ Modify the function increment by adding default parameters so that ```yml tests: - text: The result of increment(5, 2) should be 7. - testString: assert(increment(5, 2) === 7, 'The result of increment(5, 2) should be 7.'); + testString: assert(increment(5, 2) === 7); - text: The result of increment(5) should be 6. - testString: assert(increment(5) === 6, 'The result of increment(5) should be 6.'); - - text: default parameter 1 was used for value. - testString: getUserInput => assert(getUserInput('index').match(/value\s*=\s*1/g), 'default parameter 1 was used for value.'); + testString: assert(increment(5) === 6); + - text: Default parameter 1 was used for value. + testString: assert(code.match(/value\s*=\s*1/g)); ``` @@ -39,12 +39,8 @@ tests:
```js -const increment = (function() { - "use strict"; - return function increment(number, value) { - return number + value; - }; -})(); +const increment = (number, value) => number + value; + console.log(increment(5, 2)); // returns 7 console.log(increment(5)); // returns 6 ``` @@ -59,6 +55,6 @@ console.log(increment(5)); // returns 6
```js -// solution required +const increment = (number, value = 1) => number + value; ```
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.english.md index 38630922dc..3b71ac300c 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.english.md @@ -29,13 +29,13 @@ The Vegetable lets you create a vegetable object, with a property < ```yml tests: - text: Vegetable should be a class with a defined constructor method. - testString: assert(typeof Vegetable === 'function' && typeof Vegetable.constructor === 'function', 'Vegetable should be a class with a defined constructor method.'); - - text: class keyword was used. - testString: getUserInput => assert(getUserInput('index').match(/class/g),'class keyword was used.'); - - text: Vegetable can be instantiated. - testString: assert(() => {const a = new Vegetable("apple"); return typeof a === 'object';},'Vegetable can be instantiated.'); + testString: assert(typeof Vegetable === 'function' && typeof Vegetable.constructor === 'function'); + - text: class keyword should be used. + testString: assert(code.match(/class/g)); + - text: Vegetable should be able to be instantiated. + testString: assert(() => {const a = new Vegetable("apple"); return typeof a === 'object';}); - text: carrot.name should return carrot. - testString: assert(carrot.name=='carrot','carrot.name should return carrot.'); + testString: assert(carrot.name=='carrot'); ``` @@ -47,14 +47,10 @@ tests:
```js -function makeClass() { - "use strict"; - /* Alter code below this line */ +/* Alter code below this line */ + +/* Alter code above this line */ - /* Alter code above this line */ - return Vegetable; -} -const Vegetable = makeClass(); const carrot = new Vegetable('carrot'); console.log(carrot.name); // => should be 'carrot' ``` @@ -69,19 +65,11 @@ console.log(carrot.name); // => should be 'carrot'
```js -function makeClass() { - "use strict"; - /* Alter code below this line */ - class Vegetable { - constructor(name){ - this.name = name; - } +class Vegetable { + constructor(name) { + this.name = name; } - /* Alter code above this line */ - return Vegetable; } -const Vegetable = makeClass(); const carrot = new Vegetable('carrot'); -console.log(carrot.name); // => should be 'carrot' ```
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.english.md index 38974f98d9..85885294e2 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.english.md @@ -26,11 +26,11 @@ Use destructuring assignment to swap the values of a and ba should be 6, after swapping. - testString: assert(a === 6, 'Value of a should be 6, after swapping.'); + testString: assert(a === 6); - text: Value of b should be 8, after swapping. - testString: assert(b === 8, 'Value of b should be 8, after swapping.'); - - text: Use array destructuring to swap a and b. - testString: 'assert(/\[\s*(\w)\s*,\s*(\w)\s*\]\s*=\s*\[\s*\2\s*,\s*\1\s*\]/g.test(code), "Use array destructuring to swap a and b.");' + testString: assert(b === 8); + - text: Should use array destructuring to swap a and b. + testString: assert(/\[\s*(\w)\s*,\s*(\w)\s*\]\s*=\s*\[\s*\2\s*,\s*\1\s*\]/g.test(code)); ``` @@ -43,12 +43,9 @@ tests: ```js let a = 8, b = 6; -(() => { - "use strict"; - // change code below this line +// change code below this line - // change code above this line -})(); +// change code above this line console.log(a); // should be 6 console.log(b); // should be 8 ``` @@ -64,13 +61,6 @@ console.log(b); // should be 8 ```js let a = 8, b = 6; -(() => { - "use strict"; - // change code below this line - [a, b] = [b, a]; - // change code above this line -})(); -console.log(a); // should be 6 -console.log(b); // should be 8 +[a, b] = [b, a]; ``` diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.english.md index fe204276a8..c92b28a8fa 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.english.md @@ -26,11 +26,13 @@ Use destructuring assignment within the argument to the function halfstats should be an object. - testString: assert(typeof stats === 'object', 'stats should be an object.'); + testString: assert(typeof stats === 'object'); - text: half(stats) should be 28.015 - testString: assert(half(stats) === 28.015, 'half(stats) should be 28.015'); - - text: Destructuring was used. - testString: getUserInput => assert(getUserInput('index').match(/\(\s*\{\s*\w+\s*,\s*\w+\s*\}\s*\)/g), 'Destructuring was used.'); + testString: assert(half(stats) === 28.015); + - text: Destructuring should be used. + testString: assert(code.replace(/\s/g, '').match(/half=\({\w+,\w+}\)/)); + - text: Destructured parameter should be used. + testString: assert(!code.match(/stats\.max|stats\.min/)); ``` @@ -50,17 +52,11 @@ const stats = { min: -0.75, average: 35.85 }; -const half = (function() { - "use strict"; // do not change this line - // change code below this line - return function half(stats) { - // use function argument destructuring - return (stats.max + stats.min) / 2.0; - }; - // change code above this line +// change code below this line +const half = (stats) => (stats.max + stats.min) / 2.0; // use function argument destructuring +// change code above this line -})(); console.log(stats); // should be object console.log(half(stats)); // should be 28.015 ``` @@ -75,6 +71,15 @@ console.log(half(stats)); // should be 28.015
```js -// solution required +const stats = { + max: 56.78, + standard_deviation: 4.34, + median: 34.54, + mode: 23.87, + min: -0.75, + average: 35.85 +}; + +const half = ( {max, min} ) => (max + min) / 2.0; ```
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md index b739dc25dc..1e9dad6b7c 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.english.md @@ -13,8 +13,7 @@ Setter functions are meant to modify (set) the value of an object's private vari
class Book {
  constructor(author) {
    this._author = author;
  }
  // getter
  get writer(){
    return this._author;
  }
  // setter
  set writer(updatedAuthor){
    this._author = updatedAuthor;
  }
}
const lol = new Book('anonymous');
console.log(lol.writer);  // anonymous
lol.writer = 'wut';
console.log(lol.writer);  // wut
Notice the syntax we are using to invoke the getter and setter - as if they are not even functions. Getters and setters are important, because they hide internal implementation details. -

-Note:
It is a convention to precede the name of a private variable with an underscore (_). The practice itself does not make a variable private. +Note: It is a convention to precede the name of a private variable with an underscore (_). The practice itself does not make a variable private. ## Instructions @@ -22,8 +21,7 @@ Getters and setters are important, because they hide internal implementation det Use class keyword to create a Thermostat class. The constructor accepts Fahrenheit temperature. Now create getter and setter in the class, to obtain the temperature in Celsius scale. Remember that C = 5/9 * (F - 32) and F = C * 9.0 / 5 + 32, where F is the value of temperature in Fahrenheit scale, and C is the value of the same temperature in Celsius scale -Note -When you implement this, you would be tracking the temperature inside the class in one scale - either Fahrenheit or Celsius. +Note:
When you implement this, you would be tracking the temperature inside the class in one scale - either Fahrenheit or Celsius. This is the power of getter or setter - you are creating an API for another user, who would get the correct result, no matter which one you track. In other words, you are abstracting implementation details from the consumer. @@ -34,11 +32,11 @@ In other words, you are abstracting implementation details from the consumer. ```yml tests: - text: Thermostat should be a class with a defined constructor method. - testString: assert(typeof Thermostat === 'function' && typeof Thermostat.constructor === 'function','Thermostat should be a class with a defined constructor method.'); - - text: class keyword was used. - testString: getUserInput => assert(getUserInput('index').match(/class/g),'class keyword was used.'); - - text: Thermostat can be instantiated. - testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})(), 'Thermostat can be instantiated.'); + testString: assert(typeof Thermostat === 'function' && typeof Thermostat.constructor === 'function'); + - text: class keyword should be used. + testString: assert(code.match(/class/g)); + - text: Thermostat should be able to be instantiated. + testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})()); ``` @@ -50,14 +48,10 @@ tests:
```js -function makeClass() { - "use strict"; - /* Alter code below this line */ +/* Alter code below this line */ + +/* Alter code above this line */ - /* Alter code above this line */ - return Thermostat; -} -const Thermostat = makeClass(); const thermos = new Thermostat(76); // setting in Fahrenheit scale let temp = thermos.temperature; // 24.44 in C thermos.temperature = 26; @@ -74,24 +68,21 @@ temp = thermos.temperature; // 26 in C
```js -function makeClass() { - 'use strict'; - /* Alter code below this line */ - class Thermostat { - constructor(temperature) { - this._temperature = (5 / 9) * (temperature - 32); - } - get temperature() { - return this._temperature; - } - set temperature(temperature) { - this._temperature = temperature; - } + +/* Alter code below this line */ +class Thermostat { + constructor(fahrenheit) { + this._tempInCelsius = 5/9 * (fahrenheit - 32); + } + get temperature(){ + return this._tempInCelsius; + } + set temperature(newTemp){ + this._tempInCelsius = newTemp; } - /* Alter code above this line */ - return Thermostat; } -const Thermostat = makeClass(); +/* Alter code above this line */ + const thermos = new Thermostat(76); // setting in Fahrenheit scale let temp = thermos.temperature; // 24.44 in C thermos.temperature = 26; diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters.english.md index 1f6141b786..89d9b11f67 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters.english.md @@ -23,15 +23,15 @@ Modify the function sum using the rest parameter in such a way that ```yml tests: - text: The result of sum(0,1,2) should be 3 - testString: assert(sum(0,1,2) === 3, 'The result of sum(0,1,2) should be 3'); + testString: assert(sum(0,1,2) === 3); - text: The result of sum(1,2,3,4) should be 10 - testString: assert(sum(1,2,3,4) === 10, 'The result of sum(1,2,3,4) should be 10'); + testString: assert(sum(1,2,3,4) === 10); - text: The result of sum(5) should be 5 - testString: assert(sum(5) === 5, 'The result of sum(5) should be 5'); + testString: assert(sum(5) === 5); - text: The result of sum() should be 0 - testString: assert(sum() === 0, 'The result of sum() should be 0'); - - text: The sum function uses the ... spread operator on the args parameter. - testString: getUserInput => assert(getUserInput('index').match(/function\s+sum\s*\(\s*...args\s*\)\s*{/g), 'The sum function uses the ... spread operator on the args parameter.'); + testString: assert(sum() === 0); + - text: The sum function should use the ... rest parameter on the args parameter. + testString: assert(code.replace(/\s/g,'').match(/sum=\(\.\.\.args\)=>/)); ``` @@ -43,13 +43,10 @@ tests:
```js -const sum = (function() { - "use strict"; - return function sum(x, y, z) { - const args = [ x, y, z ]; - return args.reduce((a, b) => a + b, 0); - }; -})(); +const sum = (x, y, z) => { + const args = [ x, y, z ]; + return args.reduce((a, b) => a + b, 0); +} console.log(sum(1, 2, 3)); // 6 ``` @@ -63,6 +60,8 @@ console.log(sum(1, 2, 3)); // 6
```js -// solution required +const sum = (...args) => { + return args.reduce((a, b) => a + b, 0); +} ```
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.english.md index 3003f68b96..a708597fe2 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.english.md @@ -27,12 +27,12 @@ Copy all contents of arr1 into another array arr2 usin ```yml tests: - - text: arr2 is correct copy of arr1. - testString: assert(arr2.every((v, i) => v === arr1[i]), 'arr2 is correct copy of arr1.'); + - text: arr2 should be correct copy of arr1. + testString: assert(arr2.every((v, i) => v === arr1[i])); - text: ... spread operator was used to duplicate arr1. - testString: getUserInput => assert(getUserInput('index').match(/\[\s*...arr1\s*\]/g),'... spread operator was used to duplicate arr1.'); - - text: arr2 remains unchanged when arr1 is changed. - testString: assert((arr1, arr2) => {arr1.push('JUN'); return arr2.length < arr1.length},'arr2 remains unchanged when arr1 is changed.'); + testString: assert(code.match(/\[\s*...arr1\s*\]/g)); + - text: arr2 should remain unchanged when arr1 is changed. + testString: assert((arr1, arr2) => {arr1.push('JUN'); return arr2.length < arr1.length}); ``` @@ -46,10 +46,9 @@ tests: ```js const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY']; let arr2; -(function() { - "use strict"; - arr2 = []; // change this line -})(); + +arr2 = []; // change this line + console.log(arr2); ``` @@ -65,10 +64,7 @@ console.log(arr2); ```js const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY']; let arr2; -(function() { - "use strict"; - arr2 = [...arr1]; // change this line -})(); -console.log(arr2); + +arr2 = [...arr1]; ```