-
Notifications
You must be signed in to change notification settings - Fork 0
/
4) Debugging.js
100 lines (86 loc) · 2.36 KB
/
4) Debugging.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Use the JavaScript Console to Check the Value of a Variable
let a = 5;
let b = 1;
a++;
let sumAB = a + b;
console.log(a);
console.log(b);
console.log(sumAB);
// Understanding the Differences between the freeCodeCamp and Browser Console
let output = "Get this to show once in the freeCodeCamp console and not at all in the browser console";
console.log(output);
console.clear();
// Use typeof to Check the Type of a Variable
let seven = 7;
let three = "3";
console.log(seven + three);
console.log(typeof seven);
console.log(typeof three);
// Catch Misspelled Variable and Function Names
let receivables = 10;
let payables = 8;
let netWorkingCapital = receivables - payables;
console.log(`Net working capital is: ${netWorkingCapital}`);
// Catch Unclosed Parentheses, Brackets, Braces and Quotes
let 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
let innerHtml = "<p>Click here to <a href='#Home'>return home</a></p>";
console.log(innerHtml);
// Catch Use of Assignment Operator Instead of Equality Operator
let 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
function getNine() {
let x = 6;
let y = 3;
return x + y;
}
let result1 = getNine();
console.log(result1);
// Catch Arguments Passed in the Wrong Order When Calling a Function
function 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
function countToFive() {
let firstFive = "12345";
let len = firstFive.length;
for (let i = 0; i < len; i++) {
console.log(firstFive[i]);
}
}
countToFive();
// Use Caution When Reinitializing Variables Inside a Loop
function zeroArray(m, n) {
let newArray = [];
let row = [];
for (let i = 0; i < m; i++) {
row = [];
for (let j = 0; j < n; j++) {
row.push(0);
}
newArray.push(row);
}
return newArray;
}
let matrix = zeroArray(3, 2);
// Prevent Infinite Loops with a Valid Terminal Condition
function myFunc() {
for (let i = 1; i <= 4; i += 2) {
console.log("Still going!");
}
}