新版的习题将ES6单独设置成一个模块,而且重点集中在新添加的基础语法上,如:
- 新增加的let和const两个命名关键字,以及它们和var的区别,重点说了变量提升,这一部分可以参照之前写的ES6学习笔记之变量声明
- 参数的扩展,包括函数的扩展(默认参数)以及数组的扩展
- 重点讲了解构赋值
- 模板字符串的使用
- Class的基本语法
- 模块化(import,export的使用)
关于ES6的具体内容我会在ES6学习笔记中总结,争取在这个月内完成。
以下是这一部分习题的解答,其中第3题Declare a Read-Only Variable with the const Keyword有bug,目前测试无法顺利通过,正在修复中。
Introduction to the ES6 Challenges
Explore Differences Between the var and let Keywords
1
2
3
4
5
6
7
8
9
10let catName;
let quote;
function catTalk() {
"use strict";
catName = "Oliver";
quote = catName + " says Meow!";
}
catTalk();Compare Scopes of the var and let Keywords
1
2
3
4
5
6
7
8
9function checkScope() {
;
let i = "function scope";
if (true) {
let i = "block scope";
console.log("Block scope i is: ", i);
}
console.log("Function scope i is: ", i);
return i;Declare a Read-Only Variable with the const Keyword
1
2
3
4
5
6
7
8
9
10function printManyTimes(str) {
"use strict";
// change code below this line
const SENTENCE = str + " is cool!";
for(let i = 0; i < str.length; i+=2) {
console.log(SENTENCE);
}
// change code above this line
}Mutate an Array Declared with const
1
2
3
4
5
6
7
8
9
10
11
12const s = [5, 7, 2];
function editInPlace() {
"use strict";
// change code below this line
// s = [2, 5, 7]; <- this is invalid
s[0] = 2;
s[1] = 5;
s[2] = 7;
return s;
// change code above this line
}
editInPlace();-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16function freezeObj() {
"use strict";
const MATH_CONSTANTS = {
PI: 3.14
};
// change code below this line
Object.freeze(MATH_CONSTANTS);
// change code above this line
try {
MATH_CONSTANTS.PI = 99;
} catch( ex ) {
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj(); Use Arrow Functions to Write Concise Anonymous Functions
1
const magic = () => new Date();
Write Arrow Functions with Parameters
1
const myConcat = (arr1, arr2) => arr1.concat(arr2);
Write Higher Order Arrow Functions
1
const squaredIntegers = arr.filter((val) => val > 0 && val% 1 === 0).map(val => Math.pow(val, 2));
Set Default Parameters for Your Functions
1
2
3
4
5
6const increment = (function() {
"use strict";
return function increment(number, value=1) {
return number + value;
};
})();Use the Rest Operator with Function Parameters
1
2
3
4
5const sum = (function() {
"use strict";
return function sum(...args) {
return args.reduce((a, b) => a + b, 0);
};Use the Spread Operator to Evaluate Arrays In-Place
1
2
3
4
5
6const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
"use strict";
arr2 = [...arr1]; // change this line
})();Use Destructuring Assignment to Assign Variables from Objects
1
2
3
4
5
6
7
8function getLength(str) {
"use strict";
// change code below this line
const {length : len} = str; // change this
// change code above this line
return len; // you must assign length to len in line
}Use Destructuring Assignment to Assign Variables from Nested Objects
1
2
3
4
5
6
7function getMaxOfTmrw(forecast) {
"use strict";
// change code below this line
const {tomorrow: {max: maxOfTomorrow}} = forecast; // change this line
// change code above this line
return maxOfTomorrow;
}Use Destructuring Assignment to Assign Variables from Arrays
1
2
3
4
5
6
7let a = 8, b = 6;
(() => {
"use strict";
// change code below this line
[a,b] = [b,a];
// change code above this line
})();Use Destructuring Assignment with the Rest Operator to Reassign Array Elements
1
2
3
4
5
6
7function removeFirstTwo(list) {
"use strict";
// change code below this line
const [, , ...arr] = source; // change this
// change code below this line
return arr;
}Use Destructuring Assignment to Pass an Object as a Function’s Parameters
1
2
3
4
5
6
7
8
9
10
11const half = (function() {
"use strict"; // do not change this line
// change code below this line
return ({max,min}) =>
// use function argument destructuring
(stats.max + stats.min) / 2.0;
// change code above this line
})();Create Strings using Template Literals
1
2
3
4
5
6
7
8
9function makeList(arr) {
"use strict";
// change code below this line
const resultDisplayArray = arr.map((val) => `<li class="text-warning">${val}</li>`);
// change code above this line
return resultDisplayArray;
}Write Concise Object Literal Declarations Using Simple Fields
1
2
3
4
5
6
7
8const createPerson = (name, age, gender) => {
"use strict";
// change code below this line
return {
name, age, gender
};
// change code above this line
};Write Concise Declarative Functions with ES6
1
2
3
4
5
6
7const bicycle = {
gear: 2,
setGear(newGear) {
"use strict";
this.gear = newGear;
}
};Use class Syntax to Define a Constructor Function
1
2
3
4
5
6
7
8
9
10
11function makeClass() {
"use strict";
/* Alter code below this line */
class Vegetable {
constructor(name) {
this.name = name;
}
}
/* Alter code above this line */
return Vegetable;
}Use getters and setters to Control Access to an Object
1
2
3
4
5
6
7
8
9
10
11function makeClass() {
"use strict";
/* Alter code below this line */
class Thermostat {
constructor(Fa_temperature) {
this.temperature = 5/0 * (Fa_temperature-32);
}
}
/* Alter code above this line */
return Thermostat;
}Understand the Differences Between import and require
1
2
3;
import {capitalizeString} from "string_functions";
capitalizeString("hello!");Use export to Reuse a Code Block
1
2
3;
export const foo = "bar";
export const bar= "foo";Use * to Import Everything from a File
1
2;
import * as myCapitalizeStrings from "capitalize_strings";Create an Export Fallback with export default
1
2;
export default function subtract(x,y) {return x - y;}-
1
2
3;
import subtract from "math_functions";
subtract(7,4);