Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions debug-common-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Think about which debugging methods you found most useful and how you might appl
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
Expand All @@ -40,6 +45,24 @@ for (let i = 0; i < numbers.length; i++) {
}

// 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]}`);
}
}



Expand All @@ -60,3 +83,20 @@ function isPrime(num) {
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