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
83 changes: 83 additions & 0 deletions ai-detective-case-files/investigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// first case: endless loop

// code:

function mystery1(arr) { // there is a function that checks if there is any even number in the array
let i = 0; // counter declaration
while (i < arr.length) { // while loop with condition
if (arr[i] % 2 === 0) { // check if the current element is even
return true; // return true if an even number is found
}
i++; // increment the counter to avoid endless loop
}
return false;
}

// analysis: The function mystery1 is intended to check if there is any even number in the array arr. However, it contains an endless loop because the counter variable i is never incremented within the while loop. As a result, if the first element of the array is not even, the condition i < arr.length will always be true, causing the loop to run indefinitely. it must be incremented inside the loop to eventually terminate the loop when all elements have been checked.


// case 2: the cryptic function

// code:

function q(q) { return q.split('').reverse().join('') == q } // goal: check if the string is a palindrome

// problem 1: The function name 'q' is not descriptive, making it difficult to understand its purpose.
// problem 2: The parameter name 'q' is also not descriptive, which adds to the confusion.
// problem 3: The function lacks comments or documentation explaining its purpose and functionality.
// problem 4: The function does not handle edge cases, such as empty strings or non-string inputs.

// solution:

function isPalindrome(str) { // renamed function to be more descriptive
if (typeof str !== 'string') { // added type check for non-string inputs
return false; // return false for non-string inputs
}
if (str.length === 0) { // handle edge case for empty strings
return true; // an empty string is considered a palindrome
}
const reversedStr = str.split('').reverse().join(''); // reverse the string
return reversedStr === str; // check if the reversed string is equal to the original
} // added comments for clarity


// analysis: The original function was cryptic due to its non-descriptive naming and lack of comments. By renaming the function and parameter to more meaningful names, adding type checks, handling edge cases, and including comments, the revised function is now much clearer and easier to understand.

// case 3: the buggy calculator

// code:

function complicatedCalc(a, b) {
let x = a;
let y = b;
for (let i = 0; i < b; i++) {
x = x + 1;
y = y - 1;
}
return x;
}

console.log(complicatedCalc(5, 3)); // expected output: 8, actual output: 8

// problem 1: The function name 'complicatedCalc' is vague and does not convey its purpose.
// problem 2: The logic of the function is unnecessarily complex for a simple addition operation.
// problem 3: The function does not handle negative values for b, which could lead to unexpected behavior.
// problem 4: The function does not handle non-integer inputs, which could lead to errors.

// solution:

function addition(a, b) { // renamed function to be more descriptive
if (typeof a !== 'number' || typeof b !== 'number') { // added type check for non-number inputs
throw new Error('Both arguments must be numbers'); // throw error for non-number inputs
}
if (!Number.isInteger(a) || !Number.isInteger(b)) { // added check for non-integer inputs
throw new Error('Both arguments must be integers'); // throw error for non-integer inputs
}
if (b < 0) { // handle negative values for b
throw new Error('The second argument must be a non-negative integer'); // throw error for negative b
}
return a + b; // simplified logic to directly return the sum
} // added comments for clarity

// analysis: The original function was overly complicated for a simple addition operation. By renaming the function to be more descriptive, simplifying the logic, adding type checks, and handling edge cases, the revised function is now clearer, more efficient, and robust against invalid inputs. // The function now directly returns the sum of a and b, making it easier to understand and maintain.