Skip to content
Open
Show file tree
Hide file tree
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
156 changes: 156 additions & 0 deletions ai-summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
## Case 1: The Infinite Loop
✅ What the original function does
```
function mystery1(arr) {
let i = 0;
while (i < arr.length) {
if (arr[i] % 2 === 0) {
return true;
}
}
return false;
}
```
❗ Problem: this function contains a bug

Inside the while loop, i is never incremented, so:

If arr[0] is even → returns true

If arr[0] is odd → the loop runs forever → infinite loop

So the intended behavior is probably:

🎯 Check if the array contains at least one even number

But due to the missing i++, the function does not work correctly.

✅ Refactored & fixed version
Literal fix:
```
function mystery1(arr) {
let i = 0;
while (i < arr.length) {
if (arr[i] % 2 === 0) return true;
i++;
}
return false;
}
```
⭐ Cleaner & more idiomatic version
```
function hasEven(arr) {
return arr.some(n => n % 2 === 0);
}
```
🌍 Most explicit version
```
function hasEven(arr) {
for (const n of arr) {
if (n % 2 === 0) return true;
}
return false;
}
```
## Case 2: The Cryptic Function
- Asking ChatGPT
- ✅ What the function does
```
function q(q){
return q.split('').reverse().join('') == q;
}
```

q.split('') → turns the string into an array of characters

.reverse() → reverses the array

.join('') → rebuilds the reversed string

Then it compares the reversed string to the original

🎉 In short: it checks whether the string is a palindrome.

Returns true if q reads the same forwards and backwards.

Example:

"level" → true

"hello" → false

✅ How to refactor / improve it
Clearer name + cleaner code
```
function isPalindrome(str) {
return str === [...str].reverse().join('');
}
```
Using two-pointer technique (more efficient for long strings)
```
function isPalindrome(str) {
let i = 0, j = str.length - 1;
while (i < j) {
if (str[i] !== str[j]) return false;
i++;
j--;
}
return true;
}
```
Making it Unicode-safe (handles emojis / graphemes)

JavaScript .split('') breaks up multi-codepoint characters.
For robust behavior:

```
function isPalindrome(str) {
const chars = Array.from(str); // Unicode-aware
for (let i = 0, j = chars.length - 1; i < j; i++, j--) {
if (chars[i] !== chars[j]) return false;
}
return true;
}
```
## Case 3: The Over-Engineered Mess
- Asking ChatGPT "What does this function do ? How could we refactor it in a simpler manner ?"
Here’s what the function does, in simple terms.

✅ What the function actually does
```
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;
}
```
Walkthrough:

x starts as a

The loop runs b times

Each iteration increments x by 1 and decrements y by 1

But y is never used afterwards

So after b increments, the function returns:

🎉 Result = a + b

That’s all.
Everything else is unnecessary.

✅ How to refactor it
Simplest possible version
function complicatedCalc(a, b) {
return a + b;
}

👍 Optional improvements
Make it pure and explicit:
const complicatedCalc = (a, b) => a + b;
27 changes: 27 additions & 0 deletions original.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function mystery1(arr) {
let i = 0; // initialize the counter
while (i < arr.length) {
// while the counter is less than the array length
if (arr[i] % 2 === 0) {
// if the current element is even
return true; // return true
}
}
return false; // else return false
}


function q(q){return q.split('').reverse().join('')==q}
// This function checks if the input string is a palindrome by chaining multiple methods
// Not very clear, no indentation, no explicit name nor parameter description


function complicatedCalc(a, b) { // create function with 2 parameters
let x = a; // initialize x as a
let y = b; // initialize y as b
for (let i = 0; i < b; i++) { // for loop from 0 to b
x = x + 1; // increment x by 1
y = y - 1; // decrement y by 1
}
return x; // return x
}
31 changes: 31 additions & 0 deletions refactored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//********* Refactored mystery1 infinite loop *********//

function mystery1(arr) {
// declare function
let i = 0; // initialize the counter
while (i < arr.length) {
// while the counter is less than the array length
if (arr[i] % 2 === 0) {
// if the current element is even
return true; // return true
}
i++; // increment the counter and make the loop stop eventually
}
return false; // else return false
}

//********* Refactored q function *********//

// giving a name and parameter description
function isPalindrome(str) {
// This function checks if the input string is a palindrome by chaining multiple strings methods
return str === [...str].reverse().join(""); // comparing the original string with the reversed version
}

//********* Refactored complicatedCalc function *********//

function complicatedCalc(a, b) {
// create function with 2 parameters
// We could also check whether a or b are numbers
return a + b; // return statement
}