Skip to content

R50-T006: Simplify one responsibility in countLetters (CountLetters.js) [String/CountLetters.js] - #104

Open
stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T006
Open

stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T006

Conversation

@stavansomaiya

@stavansomaiya stavansomaiya commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Task R50-T006 · task 6 of 10 · Focused refactoring
Issue #6 · Simplify one responsibility in countLetters
Student Stavan Somaiya · URN e26b07f1064
File → function String/CountLetters.js → countLetters (line 9 at the pinned commit, line 9 after this PR)
Pinned source TheAlgorithms/JavaScript @ 5c39e87 · CountLetters.js#L9
Branch stavansomaiya:student/e26b07f1064/R50-T006 → S-AI-Projects:master (one commit on 5c39e87)
Outcome One readability refactor (lower-case the input once). Behaviour is identical on 306 compared inputs.

Summary

A focused refactor of one coherent calculation in String/CountLetters.js: tallying the lower-cased input.

What is made clearer: the lower-cased input is now a single named value, lowerCaseStr, computed once, and the loop walks over it directly. Previously str.toLowerCase() was recomputed twice on every iteration (in the loop condition and to read the current character) and indexed with charAt. Algorithm, return type, key order, errors, side effects and the public argument are unchanged.

Files changed

File Lines What changed
String/CountLetters.js +5 / −2 the one refactor: lowerCaseStr computed once, for...of over it
String/test/CountLetters.test.js +39 / −0 5 contract tests, written and run before the refactor
work/R50-T006/ (12 files) evidence check.py runner, README.md write-up, evidence.mjs, results.json and logs; coursework only, kept out of any upstream PR

Related Issue

Fixes: #6

This PR covers only R50-T006. Each of the 10 tasks has its own branch, commit and PR; see All 10 tasks under Additional Notes.

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • String/test/CountLetters.test.js: 5 regression tests written and run before the refactor: '' → {}; 'Mississippi' → { m: 1, i: 4, s: 4, p: 2 } with key order; underscore is counted; exact TypeError messages; a new object on each call.
  • String/CountLetters.js: const lowerCaseStr = str.toLowerCase() followed by for (const char of lowerCaseStr). This is the only change in the file; the accumulator keeps its name.
  • work/R50-T006/: check.py, before-tests.txt / after-tests.txt, an unmodified copy of the original, and evidence.mjs comparing before vs after over 306 inputs.

Screenshots / Recordings

There is no UI, so the recording is the terminal output of a fresh run on 23 Nov 2026. The run used a clean checkout of this branch with a new npm ci from the lockfile. The output is copied verbatim; only the temporary checkout path is replaced by a label.

Focused tests: npx vitest run … --reporter=verbose, 11/11 passed
$ npx vitest run String/test/CountLetters.test.js --reporter=verbose
 RUN  v1.2.1 <clean checkout of student/e26b07f1064/R50-T006>
 ✓ String/test/CountLetters.test.js > CountLetters > expect throws on use wrong param
 ✓ String/test/CountLetters.test.js > CountLetters > expect throws when using a number in the string
 ✓ String/test/CountLetters.test.js > CountLetters > expect throws when using a special characters in the string
 ✓ String/test/CountLetters.test.js > CountLetters > count the letters in a string. Allows lower case
 ✓ String/test/CountLetters.test.js > CountLetters > count the letters in a string. Allows upper case
 ✓ String/test/CountLetters.test.js > CountLetters > count the letters in a string. Allows upper and lower case
 ✓ String/test/CountLetters.test.js > countLetters contract (locked before refactor) > smallest valid input: empty string gives an empty object
 ✓ String/test/CountLetters.test.js > countLetters contract (locked before refactor) > ordinary input: counts are case-insensitive, keys in first-seen order
 ✓ String/test/CountLetters.test.js > countLetters contract (locked before refactor) > underscore is a word character, so it is counted
 ✓ String/test/CountLetters.test.js > countLetters contract (locked before refactor) > throws the documented TypeError messages
 ✓ String/test/CountLetters.test.js > countLetters contract (locked before refactor) > returns a new object on each call
 Test Files  1 passed (1)
      Tests  11 passed (11)
Automated check: python3 work/R50-T006/check.py, exit 0
$ python3 work/R50-T006/check.py
[PASS] smallest valid input
       input:    ""
       expected: {"result": {}, "keys": []}
       actual:   {"result": {}, "keys": []}
       source:   CountLetters.js:12,16,20 '' passes all guards
[PASS] ordinary input
       input:    "Mississippi"
       expected: {"result": {"m": 1, "i": 4, "s": 4, "p": 2}, "keys": ["m", "i", "s", "p"]}
       actual:   {"result": {"m": 1, "i": 4, "s": 4, "p": 2}, "keys": ["m", "i", "s", "p"]}
       source:   CountLetters.js:16,20 letters only
[PASS] mixed case
       input:    "HelLo"
       expected: {"result": {"h": 1, "e": 1, "l": 2, "o": 1}, "keys": ["h", "e", "l", "o"]}
       actual:   {"result": {"h": 1, "e": 1, "l": 2, "o": 1}, "keys": ["h", "e", "l", "o"]}
       source:   CountLetters.js:27 counting is case-insensitive
[PASS] non-string
       input:    0
       expected: {"error": "TypeError: Input should be a string"}
       actual:   {"error": "TypeError: Input should be a string"}
       source:   CountLetters.js:12-13
[PASS] special character
       input:    "two words"
       expected: {"error": "TypeError: Input must not contain special characters"}
       actual:   {"error": "TypeError: Input must not contain special characters"}
       source:   CountLetters.js:16-17
[PASS] digit
       input:    "h3llo"
       expected: {"error": "TypeError: Input must not contain numbers"}
       actual:   {"error": "TypeError: Input must not contain numbers"}
       source:   CountLetters.js:20-21
[PASS] before/after agree (results, key order, errors)
       input:    "306 inputs (fixture + 300 generated)"
       expected: []
       actual:   []
       source:   CountLetters.js:9 public signature unchanged
[KILLED] mutant case folding dropped from the refactored tally: '  const lowerCaseStr = str.toLowerCase()\n' -> '  const lowerCaseStr = str\n'
       failed:  CountLetters count the letters in a string. Allows upper case
       failed:  CountLetters count the letters in a string. Allows upper and lower case
       failed:  countLetters contract (locked before refactor) ordinary input: counts are case-insensitive, keys in first-seen order

focused tests: 11/11 passed  (node_modules/.bin/vitest run String/test/CountLetters.test.js --reporter=json --outputFile=work/R50-T006/vitest-focused.raw.json)
npm test:      11975/11975 passed  (npm test -- --reporter=json --outputFile=work/R50-T006/vitest-full.raw.json)

R50-T006: all checks passed
(exit 0)

Testing

Automated check: python3 work/R50-T006/check.py installs from the lockfile and runs the focused tests. It then prints input / expected / actual for every fixture row, runs each deliberate mutant in a temporary copy, and finally runs the repository test script in non-watch mode. It exits non-zero if any assertion or step fails, and records every command in commands.log.

To reproduce:

git fetch https://github.com/stavansomaiya/TheAlgorithms-JavaScript.git student/e26b07f1064/R50-T006
git checkout FETCH_HEAD
python3 work/R50-T006/check.py

Recorded results (committed in results.json; tool versions in versions.txt):

Check Command Result
Install from lockfile npm ci --ignore-scripts --no-audit --no-fund ✅ exit 0
Focused tests vitest run String/test/CountLetters.test.js ✅ 11/11 passed
Fixture evidence (input / expected / actual / source line) node work/R50-T006/evidence.mjs ✅ 7/7 rows match
Mutant (temporary copy): case folding dropped from the refactored tally applied by check.py ✅ caught by ordinary input: counts are case-insensitive
Repository test script, non-watch npm test (vitest run) ✅ 11975/11975 passed
Code style npm run check-style (Prettier) ✅ passes
Spelling (CI settings) codespell ✅ no new findings

Acceptance criteria → evidence:

# Done when (quoted from #6) Evidence in this PR
1 The checked behavior is tied to countLetters in String/CountLetters.js; every assertion has an explicit expected value. ✅ The 5 new tests assert literals: {}, { m: 1, i: 4, s: 4, p: 2 } with key order ['m', 'i', 's', 'p'], { a: 1, _: 1, b: 1 }, the three exact TypeError messages, and a new object on each call.
2 Use the smallest valid input and one ordinary input for countLetters from String/CountLetters.js. Write the literal inputs and independently calculated expected outputs in the tests before editing the implementation. The check must name the source line establishing each precondition. ✅ Smallest '' → {}; ordinary 'Mississippi' → { m: 1, i: 4, s: 4, p: 2 }, counted by hand. The tests were added and run before the refactor (before-tests.txt: 11/11 on the original). Preconditions cite the three guards at lines 12, 16 and 20.
3 Existing supported behavior and the public interface are preserved unless a failing regression demonstrates and documents a defect. ✅ The same 11/11 tests pass after the refactor (after-tests.txt). The full suite passes (11975/11975).
4 The before/after tests agree and the diff contains one explained readability improvement. ✅ evidence.mjs compares the new function with an unmodified copy of the original (CountLetters.before.js) on 306 inputs: 0 differences in results, key order or error messages. The diff is one explained change: lower-case the input once into lowerCaseStr, then loop over it.
5 The change is not a repository-wide formatting pass or an unmeasured optimization. ✅ Only lines 24-33 of String/CountLetters.js change, and no other file is reformatted. No performance claim is made.

Evidence files: check.py · README.md (contract, fixture, baseline, findings) · evidence.mjs · results.json · vitest-focused.json

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable. (No review feedback yet.)

Additional Notes

Before and after: the same 11/11 tests pass. The before/after comparison found 0 differences in results, key order or error messages. Guard check: removing .toLowerCase() from the new line in a temporary copy fails ordinary input: counts are case-insensitive, keys in first-seen order. This is a readability change, not a claimed optimisation and not a formatting pass.

Scope: the PR changes only String/CountLetters.js, its test file and work/R50-T006/. work/R50-T006/ is coursework evidence and would be removed from any later upstream PR.

All 10 tasks (one PR each)
Task Issue Branch Pull request
R50-T001: Lock down the contract of setBit #1 R50-T001 #99
R50-T002: Define the supported boundaries of checkKeywordValidity #2 R50-T002 #100
R50-T003: Cross-check getParentPosition #3 R50-T003 #101
R50-T004: Test an invariant of swap #4 R50-T004 #102
R50-T005: Make verification repeatable for newGeneration #5 R50-T005 #103
R50-T006: Simplify one responsibility in countLetters #6 R50-T006 #104 (this PR)
R50-T007: Publish a runnable example for DisjointSetTreeNode #7 R50-T007 #105
R50-T008: Cover the failure behavior of TrieNode #8 R50-T008 #106
R50-T009: Measure the work done by fibonacci #9 R50-T009 #107
R50-T010: Strengthen the tests of OpenKnightTour #10 R50-T010 #108

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stavan Somaiya — R50-T006: Simplify one responsibility in countLetters (CountLetters.js) [String/CountLetters.js]

1 participant