-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathdebug-common-errors.js
More file actions
102 lines (76 loc) · 3.28 KB
/
debug-common-errors.js
File metadata and controls
102 lines (76 loc) · 3.28 KB
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
101
/*
Overview
In this activity, you will receive three short JavaScript programs,
each containing a different type of error (syntax, runtime, and logic)
along with a brief explanation of what the program is supposed to do.
Your task is to identify the error, correct it, and verify the fix.
Instructions
Debugging Steps:
- Identify the error type (syntax, runtime, or logic).
- Use Debugging Techniques such as reading error messages, using console.log(), or testing in small steps.
- Propose a Fix that addresses the error.
- Verify the Solution by running the code again to ensure the program works as intended.
Reflection:
Think about which debugging methods you found most useful and how you might apply them in future projects.
*/
// Programs and Solutions
// Program A
// Description:
// This program is intended to display a simple prompt in the console but fails to run.
console.log("Welcome to the bootcamp
// What’s Wrong?
//The opening double quote " for the string is there, but the closing double quote is missing.
//Also, the comment is on the same line, so JavaScript treats everything after console.log("Welcome to the bootcamp as part of the string, causing a syntax error.
//corrected code
console.log("Welcome to the bootcamp");
// Program B
// Description:
// This code attempts to multiply each number in an array by 2 and display the results. However, it crashes at runtime.
let numbers = [2, 4, "eight"];
for (let i = 0; i < numbers.length; i++) {
let doubled = numbers[i] * 2;
console.log(doubled);
}
// What’s Wrong?
//The issue is that the array numbers contains a string "eight" instead of a number.
//When JavaScript tries to multiply "eight" by 2, it results in NaN (Not a Number), but it does not crash; instead, it prints NaN to the console.
//So why might it "crash"?
//If your runtime environment or later code expects only numbers and does not handle NaN, it could cause problems.
//corrected code
let numbers = [2, 4, 8]; // Replace "eight" with 8
for (let i = 0; i < numbers.length; i++) {
if (typeof numbers[i] === "number") {
let doubled = numbers[i] * 2;
console.log(doubled);
} else {
console.log(`Skipping non-number value: ${numbers[i]}`);
}
}
// Program C (Logic Error)
// Description:
// This snippet of code is supposed to check if a given number is prime (i.e., divisible only by 1 and itself). However, it incorrectly marks some numbers as prime or not prime.
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return true; // Supposed to indicate num is NOT prime
}
}
return false; // Supposed to indicate num IS prime
}
console.log(isPrime(7)); // Expected true but gets false
// What’s Wrong?
//The logic is flipped inside the loop.
//Right now the function returns true as soon as it finds a divisor—but true is meant to mean “is prime.”
//So every composite number (like 4 or 9) is being marked as prime, and real primes (like 7) fall through to the return false at the end.
//corrected code
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false; // number has a divisor → not prime
}
}
return true; // no divisors found → prime
}
console.log(isPrime(7)); // true