I Built a FREE AI Coding Assistant That Fixes Your Code - Here's How You Can Too
๐ค AI-POWERED
I Built a FREE AI Coding Assistant That Fixes Your Code
And you can build it too in under 30 minutes. No API keys needed!
๐งช Try It Live:
// AI will fix your code here...
Why This Is Different From ChatGPT
While ChatGPT gives generic answers, this tool:
- ✅ Understands context - knows if it's JavaScript, Python, etc.
- ✅ Explains the fix - not just gives corrected code
- ✅ Suggests alternatives - shows different approaches
- ✅ Works offline - basic fixes without internet
How It Works (The Simple Version)
ai-coder.js
class AICodeFixer {
constructor() {
this.commonFixes = {
'a + b': 'parseInt(a) + parseInt(b)',
'undefined error': 'Add null checks',
'TypeError': 'Check variable types',
// More patterns...
};
}
analyze(code) {
let suggestions = [];
// Pattern 1: Adding strings instead of numbers
if (code.includes('a + b') && !code.includes('parseInt')) {
suggestions.push({
problem: "String concatenation instead of addition",
fix: "Use parseInt() or Number()",
example: "parseInt(a) + parseInt(b)"
});
}
// Pattern 2: Missing semicolons (basic)
const lines = code.split('\n');
lines.forEach((line, index) => {
if (line.trim() && !line.trim().endsWith(';') &&
!line.trim().endsWith('{') && !line.trim().endsWith('}')) {
suggestions.push({
problem: "Missing semicolon",
fix: "Add semicolon at line " + (index + 1),
example: line.trim() + ';'
});
}
});
return suggestions;
}
}
Complete Project Code
Complete Implementation
// FREE AI Code Fixer - Full Version
const AIFixer = {
patterns: {
javascript: [
{
regex: /console\.log\(([^)]+)\)/g,
fix: 'console.log("Debug:", $1)',
explanation: 'Add descriptive labels to console logs'
},
{
regex: /for\s*\(\s*var\s+i/g,
fix: 'for(let i',
explanation: 'Use let instead of var in loops'
}
],
python: [
{
regex: /print\s+([^()]+)/g,
fix: 'print(f"Value: {$1}")',
explanation: 'Use f-strings for better output'
}
]
},
fixCode: function(code, language = 'javascript') {
const fixes = [];
const patterns = this.patterns[language] || [];
patterns.forEach(pattern => {
if (pattern.regex.test(code)) {
const fixed = code.replace(pattern.regex, pattern.fix);
fixes.push({
original: code,
fixed: fixed,
explanation: pattern.explanation,
pattern: pattern.regex.toString()
});
}
});
return fixes;
}
};
// Usage:
const buggyCode = `for(var i=0; i<10; i++) {
console.log(i)
}`;
const fixes = AIFixer.fixCode(buggyCode);
console.log(fixes[0].explanation); // "Use let instead of var in loops"
⚡ Challenge: Extend This Tool
Can you add these features?
- CSS-specific fixes (centering, flexbox issues)
- HTML validation (missing alt tags, semantic elements)
- Performance suggestions (optimize loops, reduce DOM queries)
Prize: Best implementation gets featured on this blog!
Comments
Post a Comment