Repository navigation
R50-T006: Simplify one responsibility in countLetters (CountLetters.js) [String/CountLetters.js] - #104
Open
stavansomaiya wants to merge 1 commit into
Conversation
This was referenced Nov 23, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
e26b07f1064String/CountLetters.js→countLetters(line 9 at the pinned commit, line 9 after this PR)5c39e87·CountLetters.js#L9stavansomaiya:student/e26b07f1064/R50-T006→S-AI-Projects:master(one commit on5c39e87)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. Previouslystr.toLowerCase()was recomputed twice on every iteration (in the loop condition and to read the current character) and indexed withcharAt. Algorithm, return type, key order, errors, side effects and the public argument are unchanged.Files changed
String/CountLetters.jslowerCaseStrcomputed once,for...ofover itString/test/CountLetters.test.jswork/R50-T006/(12 files)check.pyrunner,README.mdwrite-up,evidence.mjs,results.jsonand logs; coursework only, kept out of any upstream PRRelated 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
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; exactTypeErrormessages; a new object on each call.String/CountLetters.js:const lowerCaseStr = str.toLowerCase()followed byfor (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, andevidence.mjscomparing 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 cifrom 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 passedAutomated check:
python3 work/R50-T006/check.py, exit 0Testing
Automated check:
python3 work/R50-T006/check.pyinstalls 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 incommands.log.To reproduce:
Recorded results (committed in
results.json; tool versions inversions.txt):npm ci --ignore-scripts --no-audit --no-fundvitest run String/test/CountLetters.test.jsnode work/R50-T006/evidence.mjscheck.pyordinary input: counts are case-insensitivenpm test(vitest run)npm run check-style(Prettier)codespellAcceptance criteria → evidence:
countLettersin String/CountLetters.js; every assertion has an explicit expected value.{},{ m: 1, i: 4, s: 4, p: 2 }with key order['m', 'i', 's', 'p'],{ a: 1, _: 1, b: 1 }, the three exactTypeErrormessages, and a new object on each call.countLettersfrom 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.''→{}; 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.after-tests.txt). The full suite passes (11975/11975).evidence.mjscompares 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 intolowerCaseStr, then loop over it.String/CountLetters.jschange, 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.jsonChecklist
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 failsordinary 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 andwork/R50-T006/.work/R50-T006/is coursework evidence and would be removed from any later upstream PR.All 10 tasks (one PR each)
R50-T001R50-T002R50-T003R50-T004R50-T005R50-T006R50-T007R50-T008R50-T009R50-T010