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
22 changes: 17 additions & 5 deletions debug-common-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,31 @@ Think about which debugging methods you found most useful and how you might appl
// Program A
// Description:
// This program is intended to display a simple prompt in the console but fails to run.
//There's a missing closing quotation mark at the end of the string in the console.log() statement.

console.log("Welcome to the bootcamp
//The program will throw a syntax error because the string is not properly closed.
//console.log("Welcome to the bootcamp
console.log("Welcome to the bootcamp");
//Adding the missing closing quotation mark ensures the string is valid, resolving the syntax error. Always check that strings are correctly opened and closed with matching quotes.

// What’s Wrong?


// Program B
// Description:
// This code attempts to multiply each number in an array by 2 and display the results. However, it crashes at runtime.

//The array contains a string "eight" that cannot be multiplied by a number. This results in NaN (Not a Number) during runtime when attempting the multiplication.
//logic error
let numbers = [2, 4, "eight"];
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-numeric value: ${numbers[i]}`);
}
}
//This fix checks each element's type (typeof numbers[i] === "number") to ensure only numeric values are processed. Non-numeric values are skipped to prevent runtime errors.

// What’s Wrong?

Expand All @@ -46,15 +56,17 @@ for (let i = 0; i < numbers.length; 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.

//The logic incorrectly marks non-prime numbers as prime. The issue lies in the return true statement inside the for loop. It returns true as soon as a divisor is found, rather than indicating the number is not prime.
//Fix: Correct the logic to return false when a divisor is found and true only when no divisors exist:
//logic error
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 NOT prime
}
}
return false; // Supposed to indicate num IS prime
return true; // Supposed to indicate num IS prime
}

console.log(isPrime(7)); // Expected true but gets false
Expand Down