From spec to code
Mechanical translation of FRQ spec sentences, guard-first structure.
Reading a spec is a skill of its own. FRQ wording translates mechanically:
| Spec sentence | Code |
| --- | --- |
| "returns true if..." | return <condition>; โ no if needed |
| "the number of..." | accumulator loop over the data |
| "if it is not possible, return..." | guard clause first |
| "helper methods may be called" | use them โ the grid of provided helpers is a gift |
| "prepends"/"appends" | order of concatenation: s + rest vs rest + s |
| "in no particular order" | any order accepted โ keep yours simple |
A complete FRQ 2 answer is usually 5โ15 lines: the constructor copies
parameters into fields (watch for this.x = x), and each method guards,
loops, accumulates, returns. Nothing else.
Write tests from the spec, not from your code: every "returns -1 if absent" sentence is one test case; every boundary in the spec ("between 0 and 100 inclusive") is two more.