Programming Problem Solving
Test code is full of small problems: count the items in a list, find a duplicate, check an order, clean a string. This page solves four of them in JavaScript, each with a test that proves it. It also shows two traps that catch many people: the default sort and text in Bangla.
7 min read7 মিনিটে পড়া যায় · 7 sections7টি সেকশন · 12 sources12টি সোর্স · updated Nov 2026আপডেট: Nov 2026
Small problems, sharp trapsছোট সমস্যা, ধারালো ফাঁদ
Sort [10, 9, 1, 100] two ways, then count words in a Map, find a duplicate with a Set, and meet the code-unit trap.[10, 9, 1, 100] দুই উপায়ে সাজান, তারপর Map-এ শব্দ গুনুন, Set দিয়ে duplicate খুঁজুন, আর code unit-এর ফাঁদ দেখুন।
You’ll learnআপনি শিখবেন
- Why the default sort puts 100 before 9ডিফল্ট sort কেন 100-কে 9-এর আগে রাখে
- How a - b orders numbersa - b কীভাবে সংখ্যা সাজায়
- Why toSorted() keeps the inputtoSorted() কেন input অক্ষত রাখে
- Why toLowerCase() merges Test and testtoLowerCase() কেন Test আর test মেলায়
- Why length can differ from the letters seenlength কেন দেখা অক্ষরের সাথে মেলে না
What it is
Problem solving here means turning a small, exact task into code, and then proving that the code works. The proof is a test. The node:test module of Node.js creates JavaScript tests Source 8সোর্স 8. A synchronous test fails if it throws an exception, and passes otherwise Source 8সোর্স 8.
Most of these problems need only a few tools of the language. A for...of loop walks over an array, a Map, or a Set Source 11সোর্স 11. A Map holds key-value pairs Source 4সোর্স 4, and a value can occur in a Set only once Source 3সোর্স 3.
The language basics are on JavaScript for Testers. This page uses them to solve problems.
সহজ English
Problem solving here means turning a small, clear task into code, and then proving the code works with a test. Node.js has a module for tests, node:test Source 8সোর্স 8. A synchronous test fails if its code throws an error, and passes if not Source 8সোর্স 8.
Most of these problems need only a few tools. A for...of loop goes through an array, a Map, or a Set, one item at a time Source 11সোর্স 11. A Map stores pairs of keys and values Source 4সোর্স 4. A Set keeps each value only once Source 3সোর্স 3.
The basics of the language are on JavaScript for Testers. This page puts them to work.
বাংলা ব্যাখ্যা
এখানে problem solving মানে একটা ছোট, নির্দিষ্ট কাজকে কোডে রূপ দেওয়া, তারপর প্রমাণ করা যে কোডটা কাজ করে। প্রমাণ হলো একটা টেস্ট। Node.js-এর node:test module JavaScript টেস্ট তৈরি করে Source 8সোর্স 8। Synchronous টেস্ট কোনো exception throw করলে fail করে, না করলে pass Source 8সোর্স 8।
এই ধরনের বেশিরভাগ সমস্যায় language-এর অল্প কয়েকটা tool লাগে। for...of loop একটা array, Map বা Set-এর ওপর দিয়ে চলে Source 11সোর্স 11। Map key-value জোড়া রাখে Source 4সোর্স 4, আর Set-এ একটা value একবারই থাকতে পারে Source 3সোর্স 3।
Language-এর মূল বিষয়গুলো JavaScript for Testers-এ আছে। এই পেজ সেগুলো দিয়ে সমস্যা সমাধান করে।
How it works
- Counting. A
Mapholds key-value pairs Source 4সোর্স 4, so it can keep each item as a key and its count as the value. Lower-case the text first withtoLowerCase(), so thatTestandtestcount as one word Source 10সোর্স 10. - Duplicates. A value occurs in a
Setonly once Source 3সোর্স 3.firstDuplicate()below adds each item to aSetand returns the first item that is already in it. - Sorting.
sort()sorts the array in place and returns the same array Source 1সোর্স 1. By default it converts the elements to strings and compares those Source 1সোর্স 1. For numbers, pass a compare function, such as(a, b) => a - b, which subtractsbfromaSource 1সোর্স 1. A negative result putsabeforeb, and a positive one putsaafterbSource 1সোর্স 1.toSorted()is the copying version: it returns a new, sorted array Source 2সোর্স 2. - Reversing.
reverse()reverses an array in place Source 7সোর্স 7. - Text.
lengthreturns the number of UTF-16 code units, not the number of characters Source 12সোর্স 12.split("")also splits by UTF-16 code units, not by the characters that a reader sees Source 5সোর্স 5.Intl.Segmentersplits text into graphemes, words, or sentences Source 6সোর্স 6.
সহজ English
- Counting. A
Mapholds pairs of a key and a value Source 4সোর্স 4, so it can keep each word with its count. Make the text lower-case first, soTestandtestcount as the same word Source 10সোর্স 10. - Duplicates. A
Setkeeps each value once Source 3সোর্স 3.firstDuplicate()below returns the first item that is already in itsSet. - Sorting.
sort()changes the array itself Source 1সোর্স 1. By default it sorts as text, not as numbers Source 1সোর্স 1. For numbers, give it a compare function, like(a, b) => a - bSource 1সোর্স 1.toSorted()gives you a sorted copy and leaves the original alone Source 2সোর্স 2. - Reversing.
reverse()turns the array itself around Source 7সোর্স 7. - Text.
lengthcounts code units, not the letters you see Source 12সোর্স 12.split("")cuts by code units too Source 5সোর্স 5.Intl.Segmentercuts text into the pieces a reader sees Source 6সোর্স 6.
বাংলা ব্যাখ্যা
- গণনা।
Mapkey-value জোড়া রাখে Source 4সোর্স 4, তাই এতে প্রতিটা item-কে key আর তার গণনাকে value হিসেবে রাখা যায়। আগেtoLowerCase()দিয়ে text ছোট হাতের করুন, যাতেTestআরtestএকই শব্দ হিসেবে গোনা হয় Source 10সোর্স 10। - Duplicate।
Set-এ একটা value একবারই থাকে Source 3সোর্স 3। নিচেরfirstDuplicate()প্রতিটা item একটাSet-এ রাখে, আর যে item আগে থেকেই সেখানে আছে, প্রথম এমন item-টা ফেরত দেয়। - Sort।
sort()array-টাকেই সাজায় আর একই array ফেরত দেয় Source 1সোর্স 1। ডিফল্টভাবে এটা element-গুলোকে string বানিয়ে তুলনা করে Source 1সোর্স 1। Number-এর জন্য(a, b) => a - b-এর মতো একটা compare function দিন, যেটাaথেকেbবিয়োগ করে Source 1সোর্স 1। ফল negative হলেaআগে বসে, positive হলেaবসেb-এর পরে Source 1সোর্স 1।toSorted()হলো copy করা version: এটা একটা নতুন, সাজানো array দেয় Source 2সোর্স 2। - উল্টানো।
reverse()array-টাকেই উল্টে দেয় Source 7সোর্স 7। - Text।
lengthUTF-16 code unit-এর সংখ্যা দেয়, character-এর সংখ্যা নয় Source 12সোর্স 12।split("")-ও UTF-16 code unit ধরে ভাগ করে, পাঠক যে character দেখেন সেই ধরে নয় Source 5সোর্স 5।Intl.Segmentertext-কে grapheme, শব্দ বা বাক্যে ভাগ করে Source 6সোর্স 6।
Try it
Save the solutions as solutions.mjs:
export function countWords(text) {
const counts = new Map();
for (const word of text.toLowerCase().split(' ')) {
if (word === '') continue;
counts.set(word, (counts.get(word) ?? 0) + 1);
}
return counts;
}
export function firstDuplicate(items) {
const seen = new Set();
for (const item of items) {
if (seen.has(item)) return item;
seen.add(item);
}
return null;
}
export function sortNumbers(numbers) {
return numbers.toSorted((a, b) => a - b);
}
export function reverseText(text) {
const segmenter = new Intl.Segmenter('bn', { granularity: 'grapheme' });
return [...segmenter.segment(text)].map((part) => part.segment).reverse().join('');
}Save the tests next to it as solutions.test.mjs:
import test from 'node:test';
import assert from 'node:assert/strict';
import { countWords, firstDuplicate, sortNumbers, reverseText } from './solutions.mjs';
test('counts words without caring about case', () => {
const counts = countWords('Test the test TEST plan');
assert.equal(counts.get('test'), 3);
assert.equal(counts.get('plan'), 1);
});
test('finds the first duplicate, or null', () => {
assert.equal(firstDuplicate([3, 1, 4, 1, 5, 3]), 1);
assert.equal(firstDuplicate([1, 2, 3]), null);
assert.equal(firstDuplicate([]), null);
});
test('sorts numbers as numbers and keeps the input', () => {
const input = [10, 9, 1, 100];
assert.deepEqual(sortNumbers(input), [1, 9, 10, 100]);
assert.deepEqual(input, [10, 9, 1, 100]);
});
test('reverses Bangla text by what a reader sees', () => {
assert.equal(reverseText('abc'), 'cba');
assert.equal(reverseText('কি খবর'), 'রবখ কি');
});Run node --test in the folder Source 8সোর্স 8. It runs files whose names end in .test.mjs Source 8সোর্স 8, and all four tests pass. From node:assert/strict, assert.equal is strict equality Source 9সোর্স 9, and assert.deepEqual tests deep equality Source 9সোর্স 9.
Now save the two traps of this page as traps.mjs, and run node traps.mjs:
console.log([10, 9, 1, 100].sort());
console.log('কি'.length);
console.log('কি'.split('').reverse().join(''));It prints [ 1, 10, 100, 9 ], then 2, then িক: the default sort compared strings, length counted 2 code units for one letter that a reader sees, and the reversed text put the vowel sign first.
Then break each solution on purpose, for example sort with numbers.sort(), and watch which test fails. A test that never fails proves nothing.
সহজ English
Save the solutions as solutions.mjs:
export function countWords(text) {
const counts = new Map();
for (const word of text.toLowerCase().split(' ')) {
if (word === '') continue;
counts.set(word, (counts.get(word) ?? 0) + 1);
}
return counts;
}
export function firstDuplicate(items) {
const seen = new Set();
for (const item of items) {
if (seen.has(item)) return item;
seen.add(item);
}
return null;
}
export function sortNumbers(numbers) {
return numbers.toSorted((a, b) => a - b);
}
export function reverseText(text) {
const segmenter = new Intl.Segmenter('bn', { granularity: 'grapheme' });
return [...segmenter.segment(text)].map((part) => part.segment).reverse().join('');
}Save the tests as solutions.test.mjs:
import test from 'node:test';
import assert from 'node:assert/strict';
import { countWords, firstDuplicate, sortNumbers, reverseText } from './solutions.mjs';
test('counts words without caring about case', () => {
const counts = countWords('Test the test TEST plan');
assert.equal(counts.get('test'), 3);
assert.equal(counts.get('plan'), 1);
});
test('finds the first duplicate, or null', () => {
assert.equal(firstDuplicate([3, 1, 4, 1, 5, 3]), 1);
assert.equal(firstDuplicate([1, 2, 3]), null);
assert.equal(firstDuplicate([]), null);
});
test('sorts numbers as numbers and keeps the input', () => {
const input = [10, 9, 1, 100];
assert.deepEqual(sortNumbers(input), [1, 9, 10, 100]);
assert.deepEqual(input, [10, 9, 1, 100]);
});
test('reverses Bangla text by what a reader sees', () => {
assert.equal(reverseText('abc'), 'cba');
assert.equal(reverseText('কি খবর'), 'রবখ কি');
});Run node --test Source 8সোর্স 8. It finds files that end in .test.mjs Source 8সোর্স 8, and all four tests pass. From node:assert/strict, assert.equal checks strict equality, and assert.deepEqual checks deep equality Source 9সোর্স 9.
Now save the two traps as traps.mjs, and run node traps.mjs:
console.log([10, 9, 1, 100].sort());
console.log('কি'.length);
console.log('কি'.split('').reverse().join(''));You see [ 1, 10, 100, 9 ], then 2, then িক: text-style sorting, two code units for one letter, and a vowel sign that jumped to the front.
Then break a solution on purpose, for example sort with numbers.sort(), and see which test fails. A test that can never fail proves nothing.
বাংলা ব্যাখ্যা
সমাধানগুলো solutions.mjs নামে সেভ করুন:
export function countWords(text) {
const counts = new Map();
for (const word of text.toLowerCase().split(' ')) {
if (word === '') continue;
counts.set(word, (counts.get(word) ?? 0) + 1);
}
return counts;
}
export function firstDuplicate(items) {
const seen = new Set();
for (const item of items) {
if (seen.has(item)) return item;
seen.add(item);
}
return null;
}
export function sortNumbers(numbers) {
return numbers.toSorted((a, b) => a - b);
}
export function reverseText(text) {
const segmenter = new Intl.Segmenter('bn', { granularity: 'grapheme' });
return [...segmenter.segment(text)].map((part) => part.segment).reverse().join('');
}টেস্টগুলো পাশে solutions.test.mjs নামে সেভ করুন:
import test from 'node:test';
import assert from 'node:assert/strict';
import { countWords, firstDuplicate, sortNumbers, reverseText } from './solutions.mjs';
test('counts words without caring about case', () => {
const counts = countWords('Test the test TEST plan');
assert.equal(counts.get('test'), 3);
assert.equal(counts.get('plan'), 1);
});
test('finds the first duplicate, or null', () => {
assert.equal(firstDuplicate([3, 1, 4, 1, 5, 3]), 1);
assert.equal(firstDuplicate([1, 2, 3]), null);
assert.equal(firstDuplicate([]), null);
});
test('sorts numbers as numbers and keeps the input', () => {
const input = [10, 9, 1, 100];
assert.deepEqual(sortNumbers(input), [1, 9, 10, 100]);
assert.deepEqual(input, [10, 9, 1, 100]);
});
test('reverses Bangla text by what a reader sees', () => {
assert.equal(reverseText('abc'), 'cba');
assert.equal(reverseText('কি খবর'), 'রবখ কি');
});ফোল্ডারে node --test চালান Source 8সোর্স 8। এটা .test.mjs-এ শেষ হওয়া file চালায় Source 8সোর্স 8, আর চারটা টেস্টই pass করে। node:assert/strict থেকে নেওয়া assert.equal হলো strict equality Source 9সোর্স 9, আর assert.deepEqual deep equality টেস্ট করে Source 9সোর্স 9।
এবার এই পেজের দুটো ফাঁদ traps.mjs নামে সেভ করে node traps.mjs চালান:
console.log([10, 9, 1, 100].sort());
console.log('কি'.length);
console.log('কি'.split('').reverse().join(''));এটা দেখায় [ 1, 10, 100, 9 ], তারপর 2, তারপর িক: ডিফল্ট sort string তুলনা করেছে, পাঠকের চোখে একটা অক্ষরের জন্য length দুটো code unit গুনেছে, আর উল্টানো text-এ কারচিহ্ন আগে চলে এসেছে।
তারপর ইচ্ছে করে প্রতিটা সমাধান ভাঙুন, যেমন numbers.sort() দিয়ে sort করুন, আর দেখুন কোন টেস্ট fail করে। যে টেস্ট কখনো fail করে না, সেটা কিছুই প্রমাণ করে না।
How a tester uses it
- Check that a list is sorted. Read the values from the page or the API, sort a copy with
toSorted()Source 2সোর্স 2, and compare the two. Use a compare function for numbers Source 1সোর্স 1. - Find duplicates in a response. A value occurs in a
Setonly once Source 3সোর্স 3: if theSetof the ids is smaller than the list of ids, an id came back twice. - Count what came back. Count the statuses or types of a long response in a
Map, which holds key-value pairs Source 4সোর্স 4, and compare the counts with the ones you expect. - Test with Bangla text.
lengthcounts code units, not the characters that a reader sees Source 12সোর্স 12: the traps file printed2forকি.Intl.Segmentersplits text into graphemes Source 6সোর্স 6. Use it when a limit, a counter, or a cut must follow what the user sees. - Test the helpers. Test helpers are code too: give each helper a small test file. The
node:testmodule creates JavaScript tests Source 8সোর্স 8.
সহজ English
- Check that a list is sorted. Read the values, make a sorted copy with
toSorted()Source 2সোর্স 2, and compare. Use a compare function for numbers Source 1সোর্স 1. - Find duplicates. A
Setkeeps each value once Source 3সোর্স 3: if theSetof the ids is smaller than the list, an id came twice. - Count what came back. Count each status in a
Map, which holds pairs of a key and a value Source 4সোর্স 4, and compare the counts with what you expect. - Test with Bangla text.
lengthcounts code units, not the letters you see Source 12সোর্স 12: the traps file printed2forকি.Intl.Segmentercuts text into graphemes Source 6সোর্স 6. Use it when a limit or a counter must follow what the user sees. - Test the helpers. Helpers are code too: give each one a small test file.
node:testmakes JavaScript tests Source 8সোর্স 8.
বাংলা ব্যাখ্যা
- List সাজানো কি না চেক করুন। পেজ বা API থেকে value পড়ে
toSorted()দিয়ে একটা সাজানো copy বানান Source 2সোর্স 2, তারপর দুটো মেলান। Number-এর জন্য compare function দিন Source 1সোর্স 1। - Response-এ duplicate খুঁজুন।
Set-এ একটা value একবারই থাকে Source 3সোর্স 3: id-গুলোরSetযদি id-এর list-এর চেয়ে ছোট হয়, তাহলে কোনো id দুবার এসেছে। - কী এল, তা গুনুন। লম্বা response-এর status বা type-গুলো একটা
Map-এ গুনুন, যেটা key-value জোড়া রাখে Source 4সোর্স 4, তারপর গণনাগুলো প্রত্যাশিত গণনার সাথে মেলান। - বাংলা text দিয়ে টেস্ট করুন।
lengthcode unit গোনে, পাঠক যে character দেখেন তা নয় Source 12সোর্স 12: traps fileকি-এর জন্য2দেখিয়েছে।Intl.Segmentertext-কে grapheme-এ ভাগ করে Source 6সোর্স 6। কোনো limit, counter বা কাটাছেঁড়া যখন user যা দেখেন সেই ধরে চলতে হবে, তখন এটা ব্যবহার করুন। - Helper-এরও টেস্ট। টেস্টের helper-ও কোড: প্রতিটা helper-কে একটা ছোট টেস্ট file দিন।
node:testmodule JavaScript টেস্ট তৈরি করে Source 8সোর্স 8।
Common mistakes
- The default sort on numbers. The default sort compares strings Source 1সোর্স 1: the traps file printed
[ 1, 10, 100, 9 ]. Pass a compare function such as(a, b) => a - bSource 1সোর্স 1. - Changing the input by accident.
sort()andreverse()change the array in place Source 1সোর্স 1 Source 7সোর্স 7. UsetoSorted()when the caller still needs the original Source 2সোর্স 2. - Reversing text with
split(""). It splits by UTF-16 code units, not by what a reader sees Source 5সোর্স 5: the traps file turnedকিintoিক, with the vowel sign first. - Trusting
lengthfor a character limit. The value oflengthmay not match the number of characters Source 12সোর্স 12. - Testing only the happy path. Give each problem its edge cases too: an empty input, a single item, and a repeated item. The tests above check an empty list for
firstDuplicate, and a double space forcountWords.
সহজ English
- The default sort on numbers. It sorts as text Source 1সোর্স 1: the traps file printed
[ 1, 10, 100, 9 ]. Use a compare function like(a, b) => a - bSource 1সোর্স 1. - Changing the input by accident.
sort()andreverse()change the array itself Source 1সোর্স 1 Source 7সোর্স 7. UsetoSorted()to keep the original Source 2সোর্স 2. - Reversing text with
split(""). It cuts by code units, not letters Source 5সোর্স 5: the traps file turnedকিintoিক. - Trusting
lengthas a letter count. It may not match what you see Source 12সোর্স 12. - Testing only the easy case. Also try the edge cases: an empty list, one item, and a repeated item.
বাংলা ব্যাখ্যা
- Number-এ ডিফল্ট sort। ডিফল্ট sort string তুলনা করে Source 1সোর্স 1: traps file দেখিয়েছে
[ 1, 10, 100, 9 ]।(a, b) => a - b-এর মতো একটা compare function দিন Source 1সোর্স 1। - ভুল করে input বদলে ফেলা।
sort()আরreverse()array-টাকেই বদলে দেয় Source 1সোর্স 1 Source 7সোর্স 7। যিনি call করছেন তাঁর যদি আসল array লাগে, তাহলেtoSorted()ব্যবহার করুন Source 2সোর্স 2। split("")দিয়ে text উল্টানো। এটা UTF-16 code unit ধরে ভাগ করে, পাঠক যা দেখেন সেই ধরে নয় Source 5সোর্স 5: traps fileকি-কে বানিয়েছেিক, কারচিহ্ন আগে চলে এসেছে।- Character limit-এ
length-এর ওপর ভরসা।length-এর value character-এর সংখ্যার সাথে না-ও মিলতে পারে Source 12সোর্স 12। - শুধু সহজ পথ টেস্ট করা। প্রতিটা সমস্যায় edge case-ও দিন: খালি input, একটা item আর পুনরাবৃত্ত item। ওপরের টেস্টগুলো
firstDuplicate-এর জন্য খালি list আরcountWords-এর জন্য দুটো space চেক করে।
What a tester can check
Tutorial advice
This list is the tutorial's own advice, not a fact from a source.
- Say the problem back. Can you state the input, the output, and one example in one sentence each before you write code?
- Examples first. Are the examples written as tests before the solution, including the empty and the one-item case?
- The simple way first. Does a plain loop solve it before you look for a clever method?
- Inputs left alone. Does the solution leave its input unchanged, unless that is its job?
- Real text. Do the tests include Bangla text, emoji, and extra spaces, not only
abc? - A failing test. Has each test been seen to fail once, by breaking the solution on purpose?
সহজ English
Tutorial advice
This list is the tutorial's own advice, not a fact from a source.
- Say it back. Can you name the input, the output, and one example before coding?
- Examples first. Are the examples written as tests first, with an empty case?
- Simple first. Does a plain loop solve it before you try something clever?
- Inputs left alone. Is the input still the same after the call, unless changing it is the job?
- Real text. Do the tests use Bangla, emoji, and extra spaces?
- A failing test. Have you seen each test fail once?
বাংলা ব্যাখ্যা
Tutorial advice
এই তালিকা টিউটোরিয়ালের নিজের পরামর্শ, কোনো source থেকে নেওয়া তথ্য নয়।
- সমস্যাটা নিজের ভাষায় বলুন। কোড লেখার আগে input, output আর একটা উদাহরণ কি এক বাক্য করে বলতে পারেন?
- আগে উদাহরণ। খালি আর একটা item-এর case-সহ উদাহরণগুলো কি সমাধানের আগে টেস্ট হিসেবে লেখা?
- আগে সহজ পথ। চালাক কোনো পদ্ধতি খোঁজার আগে সাধারণ একটা loop দিয়ে কি সমাধান হয়?
- Input অক্ষত। কাজটাই যদি না হয়, তাহলে সমাধান কি input বদলায় না?
- আসল text। টেস্টে কি শুধু
abcনয়, বাংলা text, emoji আর বাড়তি space-ও আছে? - একবার fail করা টেস্ট। ইচ্ছে করে সমাধান ভেঙে প্রতিটা টেস্টকে কি একবার fail করতে দেখেছেন?
Key words
| Term | Meaning |
|---|---|
Map | An object that holds key-value pairs. |
Set | A collection in which each value occurs once; good for duplicates. |
sort() | Sorts in place; by default it compares the elements as strings. |
| Compare function | (a, b) => a - b: negative puts a first, positive puts b first. |
toSorted() | Returns a sorted copy and leaves the original unchanged. |
for...of | A loop over the items of an array, a Map, or a Set. |
| Code unit | A UTF-16 code unit of a JavaScript string; length counts these. |
| Grapheme | A unit that Intl.Segmenter can split text into, next to words and sentences. |
Intl.Segmenter | Splits text into graphemes, words, or sentences. |
node:test | The test runner of Node.js; run it with node --test. |
| Edge case | An unusual input that a solution must also handle, such as an empty input, a single item, or a repeated item. |