What you'll be able to do
By the end of this page you should be able to approach small practice problems with a calm process, solve a few warmup tasks, and know how to check your own answers.
Previous post: testing basics for JS functions.
Who this is for
- People who freeze when a problem has no tutorial GIF
- Juniors preparing for light interviews or homework drills
- Anyone who finished syntax posts and needs reps
You can skip the specific prompts if you already drill daily — still steal the process. This opens Phase 12 — practice.
A calm process (use every time)
- Restate the input and output in one sentence.
- Do one tiny example on paper or in comments.
- Write the simplest correct version.
- Test edge cases (empty, one item, weird types if relevant).
Scenario A — "sum an array." Input: numbers. Output: one number. Empty → 0.
Scenario B — "reverse a string." Input: text. Output: text. Empty → "".
Warmup 1 — sum
function sum(nums) {
let total = 0;
for (let i = 0; i < nums.length; i++) total += nums[i];
return total;
}
Scenario A — [1,2,3] → 6.
Scenario B — [] → 0.
Warmup 2 — count vowels
function countVowels(str) {
const vowels = "aeiou";
let count = 0;
const lower = str.toLowerCase();
for (let i = 0; i < lower.length; i++) {
if (vowels.includes(lower[i])) count++;
}
return count;
}
Scenario A — "Hello" → 2.
Scenario B — "why" → 0 with this simple aeiou set (y rules vary — define yours).
Warmup 3 — unique values
function unique(arr) {
const seen = new Set();
const out = [];
for (const item of arr) {
if (!seen.has(item)) {
seen.add(item);
out.push(item);
}
}
return out;
}
Scenario A — keep first-seen order.
Scenario B — [1,1,2] → [1,2].
How to practice without cheating yourself
Scenario A — peek at a solution after 20 focused minutes, then rewrite from memory the next day.
Scenario B — only read solutions. You get the illusion of skill; timed recalls fix that.
A tiny practice file
<!DOCTYPE html>
<html>
<body>
<pre id="out"></pre>
<script>
function sum(nums) {
let total = 0;
for (let i = 0; i < nums.length; i++) total += nums[i];
return total;
}
function assertEqual(a, e, label) {
if (a !== e) throw new Error(label + " failed");
}
try {
assertEqual(sum([1, 2, 3]), 6, "sum");
assertEqual(sum([]), 0, "empty");
document.getElementById("out").textContent = "warmup checks passed";
} catch (err) {
document.getElementById("out").textContent = String(err.message);
}
</script>
</body>
</html>
Add asserts for vowels and unique yourself.
Mistakes I see a lot
1. Optimizing before a correct answer exists.
2. Ignoring empty inputs.
3. Copying a clever one-liner you cannot explain in an interview.
4. Practicing only in your head — type it.
What to try before the next post
- Solve the three warmups without looking back.
- Invent one more string problem and write asserts.
- Timebox 25 minutes of practice three days this week.
Next in this series: mini project — todo DOM — put events, arrays, and rendering together.
Try this next outside the series
Practice goes further when you test the result and compare revisions instead of solving once and forgetting.
- React Testing Library basics — carry your practice mindset into UI-level checks
- Diff Checker — compare your first and second solution to spot cleaner code