这部分主要列举了一些实际写代码过程中容易犯的错误和测试方法.
Debugging也是新版习题里新添加的内容,这一部分内容没什么难度,主要列举了一些实际写代码过程中容易犯的错误和测试方法:
- 使用
console.log()
来输出内容- 当输出内容较多(如循环导致内容重复出现)时,可以使用
console.clear()
来清空控制台- 可以使用
typeof
来测试变量的类型- 常见错误(主要为笔误)有:
- 变量及函数名拼写错误
- 括号(包括中括号和大括号)及引号不完整
- 单引号、双引号混用
- 将赋值符
=
错用成等号==
或===
- 调用函数时遗漏
()
- 调用函数时传参错误(顺序错误)
- OBOE错误(Off By One Errors),特地查了一下,中文翻译叫差一错误,表示在使用循环时对条件的设置有偏差,导致结果多1个或少1个
- 循环套循环时需要谨慎
- 循环时条件设置错误导致无限循环
以上错误其实还挺容易犯的,特别是刚开始学编程的时候,比如赋值符合等号弄错,调用函数遗漏括号,这些错误我都犯过,而且可能还不止一次,其实归根到底是对知识点理解不透彻,现在回头再来学一遍基础知识,感觉查缺补漏还是很有必要的。
以下是这一部分的习题解答:
Introduction to the Debugging Challenges
Use the JavaScript Console to Check the Value of a Variable
1
2
3
4
5
6
7
8let a = 5;
let b = 1;
a++;
// Add your code below this line
console.log(a);
let sumAB = a + b;
console.log(sumAB);Understanding the Differences between the freeCodeCamp and Browser Console
1
2
3
4
5
6
7
8
9
10
11// Open your browser console
let outputTwo = "This will print to the browser console 2 times";
// Use console.log() to print the outputTwo variable
console.log(outputTwo);
let outputOne = "Try to get this to log only once to the browser console";
// Use console.clear() in the next line to print the outputOne only once
console.clear();
// Use console.log() to print the outputOne variable
console.log(outputOne);Use typeof to Check the Type of a Variable
1
2
3
4
5
6let seven = 7;
let three = "3";
console.log(seven + three);
// Add your code below this line
console.log(typeof seven);
console.log(typeof three);Catch Misspelled Variable and Function Names
1
2
3
4let receivables = 10;
let payables = 8;
let netWorkingCapital = receivables - payables;
console.log(`Net working capital is: ${netWorkingCapital}`);Catch Unclosed Parentheses, Brackets, Braces and Quotes
1
2
3let myArray = [1, 2, 3];
let arraySum = myArray.reduce((previous, current) => previous + current);
console.log(`Sum of array values is: ${arraySum}`);Catch Mixed Usage of Single and Double Quotes
1
2let innerHtml = "<p>Click here to <a href='#Home'>return home</a></p>";
console.log(innerHtml);Catch Use of Assignment Operator Instead of Equality Operator
1
2
3
4
5
6
7
8
9
10
11let x = 7;
let y = 9;
let result = "to come";
if(x === y) {
result = "Equal!";
} else {
result = "Not equal!";
}
console.log(result);Catch Missing Open and Closing Parenthesis After a Function Call
1
2
3
4
5
6
7
8function getNine() {
let x = 6;
let y = 3;
return x + y;
}
let result = getNine();
console.log(result);Catch Arguments Passed in the Wrong Order When Calling a Function
1
2
3
4
5
6
7
8function raiseToPower(b, e) {
return Math.pow(b, e);
}
let base = 2;
let exp = 3;
let power = raiseToPower(base, exp);
console.log(power);Catch Off By One Errors When Using Indexing
1
2
3
4
5
6
7
8
9
10
11function countToFive() {
let firstFive = "12345";
let len = firstFive.length;
// Fix the line below
for (let i = 0; i < len; i++) {
// Do not alter code below this line
console.log(firstFive[i]);
}
}
countToFive();Use Caution When Reinitializing Variables Inside a Loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18function zeroArray(m, n) {
// Creates a 2-D array with m rows and n columns of zeroes
let newArray = [];
let row = [];
for (let j = 0; j < n; j++) {
// Pushes n zeroes into the current row to create the columns
row.push(0);
}
for (let i = 0; i < m; i++) {
// Adds the m-th row into newArray
// Pushes the current row, which now has n zeroes in it, to the array
newArray.push(row);
}
return newArray;
}
let matrix = zeroArray(3, 2);
console.log(matrix);Prevent Infinite Loops with a Valid Terminal Condition
1
2
3
4
5function myFunc() {
for (let i = 1; i <= 4; i += 2) {
console.log("Still going!");
}
}