15 AI Coding Exercises with Verified Solutions
AI coding exercises with fifteen original JavaScript tasks, sample solutions, expected outputs and edge cases for code-review practice.
Learn the concept, work through an example, and check the result.
Core idea
A solution is useful when you can test and explain it.
Test → inspect
Decide → commit
The direct answer
Practise fifteen small changes with explicit checks.
AI coding exercises are useful when you attempt the contract first and independently check the proposed answer.
These fifteen original JavaScript exercises are designed for AI-assisted practice. Attempt each task yourself, ask an assistant for a narrow proposal if useful, then compare the proposed result with the sample solution and edge case. The examples are educational snippets, not drop-in production utilities.
Course pricing follows the overview. Use a current JavaScript runtime for experiments and inspect any suggestion before running it in a real repository.
Course access
Choose your next step in GitHub Copilot.
Review the current course options and access details here.
Heycademy Courses · Coming Soon
GitHub Copilot & AI Coding Productivity
We’re preparing an interactive learning experience with practical exercises, skill assessments and personalized learning to help you build confidence and master new skills.
We’re putting the finishing touches on this course. Please check back for updates.
Topic in practice
AI coding exercises in a real decision.
A sample solution can still have a production limitation. The group-totals example needs a safer key strategy for untrusted input, and the date example requires an explicit timezone rule.
As you work through the case, identify the next action, the evidence required and what remains uncertain. The GitHub documentation provides background for checking the decision.
Practice method
Specify, attempt, test, then explain.
For each exercise, write the input and expected output before asking for code. Run the normal case and edge case, then explain why the solution follows the contract. Do not accept a test merely because it passes: validate the expected answer independently.
| Step | Evidence |
|---|---|
| Contract | Input, output and error rule. |
| Proposal | Small function only. |
| Check | Normal and edge examples. |
| Review | Explain limitations and repository fit. |
Exercises 1–5
Parsing and collection basics.
Task: implement the named behavior. Expected: ” Ada Lovelace ” → “Ada Lovelace”
function normalizeName(s){return s.trim().replace(/ +/g," ");}Review edge: Whitespace-only input becomes an empty string; decide if that is allowed.
Task: implement the named behavior. Expected: “7” → 7; “7.2” throws
function quantity(s){if(!/^[1-9][0-9]*$/.test(s))throw Error("invalid");const n=Number(s);if(!Number.isSafeInteger(n))throw Error("invalid");return n;}Review edge: Unsafe integers also throw.
Task: implement the named behavior. Expected: [2,2,3] → [2,3]
function uniqueIds(xs){return [...new Set(xs)];}Review edge: Order is first occurrence.
Task: implement the named behavior. Expected: [{status:”open”},{status:”closed”}] → 1
function countOpen(xs){return xs.filter(x=>x.status==="open").length;}Review edge: Missing status is not open.
Task: implement the named behavior. Expected: [2,4] → 3
function average(xs){if(!xs.length)throw Error("empty");return xs.reduce((a,b)=>a+b,0)/xs.length;}Review edge: Empty input throws.
Keep building this skill
Turn the example into a skill you can use.
Return to the course options whenever you are ready.
Exercises 6–10
Aggregation and transformation checks.
Task: implement the named behavior. Expected: [{key:”a”,value:2},{key:”a”,value:3}] → {a:5}
function totals(xs){return xs.reduce((a,x)=>(a[x.key]=(a[x.key]||0)+x.value,a),{});}Review edge: Use Map for untrusted keys such as __proto__.
Task: implement the named behavior. Expected: -2 → 0; 102 → 100
function clamp(n){return Math.min(100,Math.max(0,n));}Review edge: NaN needs separate validation.
Task: implement the named behavior. Expected: “Hello World!” → “hello-world”
function slug(s){return s.trim().toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"");}Review edge: Non-ASCII characters need a product rule.
Task: implement the named behavior. Expected: [1,2,3], 2 → [[1,2],[3]]
function chunks(xs,n){if(!Number.isInteger(n)||n<1)throw Error("size");return Array.from({length:Math.ceil(xs.length/n)},(_,i)=>xs.slice(i*n,(i+1)*n));}Review edge: Zero size throws.
Task: implement the named behavior. Expected: “false” → false
function flag(s){if(s==="true")return true;if(s==="false")return false;throw Error("flag");}Review edge: Case policy must be explicit.
Exercises 11–15
Access, dates and output boundaries.
Task: implement the named behavior. Expected: {roles:[“read”]}, “read” → true
function allowed(user,role){return Array.isArray(user.roles)&&user.roles.includes(role);}Review edge: Do not use this alone as an authorization boundary.
Task: implement the named behavior. Expected: UTC date → YYYY-MM-DD
function dateKey(d){if(!(d instanceof Date)||Number.isNaN(d.getTime()))throw Error("date");return d.toISOString().slice(0,10);}Review edge: Local date boundaries differ.
Task: implement the named behavior. Expected: 5 → 3
function retries(n){if(!Number.isInteger(n)||n<0)throw Error("retries");return Math.min(n,3);}Review edge: Document whether zero is valid.
Task: implement the named behavior. Expected: “<" → "<"
function escapeHtml(s){return s.replace(/[&<>"]/g,c=>({"&":"&","<":"<",">":">","\"":"""}[c]));}Review edge: Use framework escaping for real HTML contexts.
Task: implement the named behavior. Expected: {id:1,name:”A”,secret:”x”} → {id:1,name:”A”}
function pickName(x){return {id:x.id,name:x.name};}Review edge: Validate inputs before API output.
After the exercises
Check the limits of each snippet.
The group-totals snippet uses a plain object and is unsafe for untrusted keys such as __proto__. The HTML escape example handles one text context; a production interface should use its framework’s context-aware escaping. The permission example is a display-level check, not a complete authorization system. These limitations are part of the answer, not optional footnotes.
The testing strategy shows how to turn a selected exercise into a repository-quality change.
Exercise review
A sample function is a starting point.
Pick exercise 6, group totals. The sample implementation uses a plain object. In JavaScript, keys such as __proto__ can interact with inherited object properties. If keys come from an untrusted source, a Map or an object with no prototype is a safer design. Write a test with that key, then decide the expected output format before changing the implementation. This is why an edge note accompanies each snippet.
For exercise 12, date keys, decide whether the product needs a UTC calendar day or a user-local day. A Date near midnight can have different dates in those time zones. The example uses toISOString(), so its contract is UTC. If the business asks for a local day, change the test fixtures and implementation together. A generated solution that appears correct on a midday fixture can still fail at the boundary.
FAQ
Questions about this skill.
Are these official Copilot prompts?
No. They are original learning exercises.
Are all snippets production ready?
No. Each names an edge or limitation requiring further design.
Can I use another language?
Yes, but preserve the contract and independently verify outputs.
Continue with the course
Build reliable AI-assisted coding skills.
Review the course options and choose your next learning step.