Skip to contentমূল লেখায় যান

Step 06 · SDET roadmapধাপ 06 · SDET roadmap

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-এর ফাঁদ দেখুন।

3 min3 মিনিট8 steps8টি ধাপ
  • Orderক্রম10, 9, 1, 100
  • Out of orderক্রমহীন জোড়া2
  • Compareতুলনাtext
arrayঅ্যারে4 numbers4টা সংখ্যাsort()sort()as textটেক্সট হিসেবেresultফল10109911100100out of order · 2ক্রমহীন জোড়া · 24 numbers4টা সংখ্যা

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

সহজ English

বাংলা ব্যাখ্যা

  • গণনা। Map key-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। length UTF-16 code unit-এর সংখ্যা দেয়, character-এর সংখ্যা নয় Source 12সোর্স 12। split("")-ও UTF-16 code unit ধরে ভাগ করে, পাঠক যে character দেখেন সেই ধরে নয় Source 5সোর্স 5। Intl.Segmenter text-কে 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 Set only once Source 3সোর্স 3: if the Set of 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. length counts code units, not the characters that a reader sees Source 12সোর্স 12: the traps file printed 2 for কি. Intl.Segmenter splits 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:test module creates JavaScript tests Source 8সোর্স 8.

সহজ English

বাংলা ব্যাখ্যা

  • 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 দিয়ে টেস্ট করুন। length code unit গোনে, পাঠক যে character দেখেন তা নয় Source 12সোর্স 12: traps file কি-এর জন্য 2 দেখিয়েছে। Intl.Segmenter text-কে grapheme-এ ভাগ করে Source 6সোর্স 6। কোনো limit, counter বা কাটাছেঁড়া যখন user যা দেখেন সেই ধরে চলতে হবে, তখন এটা ব্যবহার করুন।
  • Helper-এরও টেস্ট। টেস্টের helper-ও কোড: প্রতিটা helper-কে একটা ছোট টেস্ট file দিন। node:test module JavaScript টেস্ট তৈরি করে Source 8সোর্স 8।

Common mistakes

সহজ English

বাংলা ব্যাখ্যা

  • 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

TermMeaning
MapAn object that holds key-value pairs.
SetA 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...ofA loop over the items of an array, a Map, or a Set.
Code unitA UTF-16 code unit of a JavaScript string; length counts these.
GraphemeA unit that Intl.Segmenter can split text into, next to words and sentences.
Intl.SegmenterSplits text into graphemes, words, or sentences.
node:testThe test runner of Node.js; run it with node --test.
Edge caseAn unusual input that a solution must also handle, such as an empty input, a single item, or a repeated item.
  1. [1]
    Array.prototype.sort()

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  2. [2]
    Array.prototype.toSorted()

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  3. [3]
    Set

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  4. [4]
    Map

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  5. [5]
    String.prototype.split()

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  6. [6]
    Intl.Segmenter

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  7. [7]
    Array.prototype.reverse()

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  8. [8]
    Test runner

    Node.js (OpenJS Foundation) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  9. [9]
    Assert

    Node.js (OpenJS Foundation) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  10. [10]
    String.prototype.toLowerCase()

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  11. [11]
    Loops and iteration

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15

  12. [12]
    String: length

    MDN Web Docs (Mozilla) · accessed 2026-11-15দেখা হয়েছে: 2026-11-15