DEV Community

Cover image for I Asked AI to Fix One JavaScript Problem and Gave It the Whole Kitchen πŸ€–πŸ³
Siddhartha Ghosh
Siddhartha Ghosh

Posted on

I Asked AI to Fix One JavaScript Problem and Gave It the Whole Kitchen πŸ€–πŸ³

I am not a full-time developer. I work mainly with market analysis and digital marketing, but sometimes I use HTML and JavaScript for small website tasks.

Recently, I faced a very simple JavaScript problem.

I had this button:

<button id="startBtn">Start Free Trial</button>
Enter fullscreen mode Exit fullscreen mode

And this JavaScript:

document.getElementById("startButton").addEventListener("click", () => {
  alert("Trial started!");
});
Enter fullscreen mode Exit fullscreen mode

The button was not working.

The problem was actually very small. The HTML used startBtn, but the JavaScript was searching for startButton.

The correct code was:

document.getElementById("startBtn").addEventListener("click", () => {
  alert("Trial started!");
});
Enter fullscreen mode Exit fullscreen mode

Simple, right?

But instead of sharing only these few lines with AI, I pasted my entire JavaScript file.

It included menu functions, form validation, tracking code, popups, and several other things that had nothing to do with the button.

Basically, AI asked for one spoon, and I gave it the whole kitchen. πŸ˜…

What happened next?

The AI started reading everything.

It gave me a long explanation, suggested changes to unrelated functions, and used a lot more context than necessary.

At one point, I was thinking:

β€œWhy is this becoming so complicated? I only wanted the button to work.”

Then I realized the problem was not only the AI.

The problem was how I gave the information.

I thought more code would help the AI understand the situation better. But more code also created more noise.

The real issue was hidden inside hundreds of unnecessary lines.

What I do now

Now, before asking AI for help, I try to share only three things.

First, I share the exact code that is not working.

Second, I explain what is happening.

Third, I explain what I expected to happen.

For example:

β€œThis button should show an alert when I click it, but nothing happens. Can you find the problem?”

Then I include only the related HTML and JavaScript.

This small change usually gives me a faster and clearer answer.

It also saves tokens, especially when I am using tools with usage limits.

More context is not always better context

Large context windows sound impressive. We often hear that AI models can read thousands of lines of code, long reports, and full projects.

But just because AI can read everything does not mean we should send everything.

It is similar to asking someone to find one sentence in a book.

You could give them the whole library.

Or you could tell them the book, chapter, and page.

The second option is usually better. πŸ“–

I think context management is becoming an important AI skill.

It is not only about writing better prompts. It is also about knowing what information the AI actually needs.

For small coding problems, that may mean sharing one function instead of the whole file.

For a business report, it may mean sharing one important section instead of a 100 page document.

For data analysis, it may mean showing the useful columns instead of the entire database.

But what about large projects?

Manually selecting code is easy when the problem is small.

But what happens when the project has many files, functions, and dependencies?

Sometimes one function depends on another file. That file may depend on a third file. In that situation, sharing only a small code block may not be enough.

This is why tools that create a map of the codebase sound useful.

Instead of forcing AI to read every file, the tool can help it find the relevant function, file, and connection.

That seems much smarter than repeatedly pasting the full project.

Still, I am learning.

How do you manage context when using ChatGPT, Claude, or another AI coding tool?

Do you manually select the important code, or do you use a tool that helps AI understand the whole project without reading everything? πŸ‘‡

Top comments (0)