Skip to content

Commit fe2a044

Browse files
committed
ES6 (ex. 13 to 29)
+ Finished ES6 module
1 parent dbd75cd commit fe2a044

19 files changed

+345
-18
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements:
3+
Use destructuring assignment with the rest parameter to perform an effective Array.prototype.slice() so that arr is a
4+
sub-array of the original array source with the first two elements omitted.
5+
6+
- arr should be [3,4,5,6,7,8,9,10]
7+
- source should be [1,2,3,4,5,6,7,8,9,10]
8+
- Array.slice() should not be used.
9+
- Destructuring on list should be used.
10+
*/
11+
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
12+
13+
function removeFirstTwo(list) {
14+
// Only changed code below this line
15+
const [first, second, ...arr] = list;
16+
// Only changed code above this line
17+
return arr;
18+
}
19+
20+
const arr = removeFirstTwo(source);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Use Destructuring Assignment to Pass an Object as a Function's Parameters:
3+
Use destructuring assignment within the argument to the function half to send only max and min inside the function.
4+
5+
- stats should be an object.
6+
- half(stats) should be 28.015
7+
- Destructuring should be used.
8+
- Destructured parameter should be used.
9+
*/
10+
const stats = {
11+
max: 56.78,
12+
standard_deviation: 4.34,
13+
median: 34.54,
14+
mode: 23.87,
15+
min: -0.75,
16+
average: 35.85
17+
};
18+
19+
// Only changed code below this line
20+
const half = ({max, min}) => (max + min) / 2.0;
21+
// Only changed code above this line
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Create Strings using Template Literals:
3+
Use template literal syntax with backticks to create an array of list element (li) strings.
4+
Each list element's text should be one of the array elements from the failure property on the result object and have a
5+
class attribute with the value text-warning.
6+
The makeList function should return the array of list item strings.
7+
Use an iterator method (any kind of loop) to get the desired output (shown below).
8+
9+
[
10+
'<li class="text-warning">no-var</li>',
11+
'<li class="text-warning">var-on-top</li>',
12+
'<li class="text-warning">linebreak</li>'
13+
]
14+
15+
- failuresList should be an array containing result failure messages.
16+
- failuresList should be equal to the specified output.
17+
- Template strings and expression interpolation should be used.
18+
- An iterator should be used.
19+
*/
20+
const result = {
21+
success: ["max-length", "no-amd", "prefer-arrow-functions"],
22+
failure: ["no-var", "var-on-top", "linebreak"],
23+
skipped: ["no-extra-semi", "no-dup-keys"]
24+
};
25+
26+
function makeList(arr) {
27+
// Only changed code below this line
28+
const failureItems = arr.map(value => `<li class="text-warning">${value}</li>`);
29+
// Only changed code above this line
30+
return failureItems;
31+
}
32+
33+
const failuresList = makeList(result.failure);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Write Concise Object Literal Declarations Using Object Property Shorthand:
3+
Use object property shorthand with object literals to create and return an object with name, age and gender properties.
4+
5+
- createPerson("Zodiac Hasbro", 56, "male") should return {name: "Zodiac Hasbro", age: 56, gender: "male"}.
6+
- Your code should not use key:value.
7+
*/
8+
const createPerson = (name, age, gender) => {
9+
// Only changed code below this line
10+
return ({name, age, gender});
11+
// Only changed code above this line
12+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Write Concise Declarative Functions with ES6:
3+
Refactor the function setGear inside the object bicycle to use the shorthand syntax described above.
4+
5+
- Traditional function expression should not be used.
6+
- setGear should be a declarative function.
7+
- bicycle.setGear(48) should change the gear value to 48.
8+
*/
9+
// Only changed code below this line
10+
const bicycle = {
11+
gear: 2,
12+
setGear(newGear) {
13+
this.gear = newGear;
14+
}
15+
}
16+
;
17+
// Only changed code above this line
18+
bicycle.setGear(3);
19+
console.log(bicycle.gear);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Use class Syntax to Define a Constructor Function:
3+
Use the class keyword and write a constructor to create the Vegetable class.
4+
The Vegetable class allows you to create a vegetable object with a property name that gets passed to the constructor.
5+
6+
- Vegetable should be a class with a defined constructor method.
7+
- The class keyword should be used.
8+
- Vegetable should be able to be instantiated.
9+
- carrot.name should return carrot.
10+
*/
11+
12+
// Only changed code below this line
13+
class Vegetable {
14+
constructor(name) {
15+
this.name = name;
16+
}
17+
}
18+
19+
// Only changed code above this line
20+
21+
const carrot = new Vegetable('carrot');
22+
console.log(carrot.name); // Should display 'carrot'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Use getters and setters to Control Access to an Object:
3+
Use the class keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature.
4+
In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.
5+
Remember that C = 5/9 * (F - 32) and F = C * 9.0 / 5 + 32, where F is the value of temperature in Fahrenheit, and C
6+
isthe value of the same temperature in Celsius.
7+
8+
Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.
9+
10+
This is the power of a getter and a setter. You are creating an API for another user, who can get the correct result
11+
regardless of which one you track.
12+
In other words, you are abstracting implementation details from the user.
13+
14+
- Thermostat should be a class with a defined constructor method.
15+
- class keyword should be used.
16+
- Thermostat should be able to be instantiated.
17+
- When instantiated with a Fahrenheit value, Thermostat should set the correct temperature.
18+
- A getter should be defined.
19+
- A setter should be defined.
20+
- Calling the setter with a Celsius value should set the temperature.
21+
*/
22+
23+
// Only change coded below this line
24+
class Thermostat {
25+
constructor(temperature) {
26+
this._temperature = temperature;
27+
}
28+
29+
get temperature() {
30+
return 5 / 9 * (this._temperature - 32);
31+
}
32+
33+
set temperature(temperature) {
34+
this._temperature = temperature * 9.0 / 5 + 32;
35+
}
36+
}
37+
38+
// Only change coded above this line
39+
40+
const thermos = new Thermostat(76); // Setting in Fahrenheit scale
41+
let temp = thermos.temperature; // 24.44 in Celsius
42+
console.log(temp);
43+
thermos.temperature = 26;
44+
console.log(thermos);
45+
temp = thermos.temperature; // 26 in Celsius
46+
console.log(temp);

02-es6/20-create-a-module-script.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--
2+
Create a Module Script:
3+
Add a script to the HTML document of type module and give it the source file of index.js
4+
5+
- You should create a script tag.
6+
- Your script tag should have the type attribute with a value of module.
7+
- Your script tag should have a src of index.js.
8+
-->
9+
<html>
10+
<body>
11+
<!-- Only changed code below this line -->
12+
<script type="module" src="index.js"></script>
13+
<!-- Only changed code above this line -->
14+
</body>
15+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Use export to Share a Code Block:
3+
There are two string-related functions in the editor. Export both of them using the method of your choice.
4+
5+
- You should properly export uppercaseString.
6+
- You should properly export lowercaseString.
7+
*/
8+
const uppercaseString = (string) => {
9+
return string.toUpperCase();
10+
}
11+
12+
const lowercaseString = (string) => {
13+
return string.toLowerCase()
14+
}
15+
16+
export {uppercaseString, lowercaseString};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Reuse JavaScript Code Using import:
3+
Add the appropriate import statement that will allow the current file to use the uppercaseString and lowercaseString
4+
functions you exported in the previous lesson. These functions are in a file called string_functions.js, which is in
5+
the same directory as the current file.
6+
7+
- You should properly import uppercaseString.
8+
- You should properly import lowercaseString.
9+
*/
10+
11+
import {lowercaseString, uppercaseString} from "./21-use-export-to-share-a-code-block";
12+
// Only change coded above this line
13+
14+
uppercaseString("hello");
15+
lowercaseString("WORLD!");

0 commit comments

Comments
 (0)