All files / web/src/lib/audio termsToSentence.ts

100% Statements 26/26
100% Branches 5/5
100% Functions 1/1
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x  
import { numberToEnglish } from './numberToEnglish'
 
/**
 * Convert a problem's terms into a readable English sentence.
 *
 * Terms follow the convention: first term is the starting number,
 * subsequent positive terms mean addition, negative terms mean subtraction.
 *
 * Examples:
 *   termsToSentence([5, 3])     → "five plus three"
 *   termsToSentence([10, -3])   → "ten minus three"
 *   termsToSentence([5, 3, -2]) → "five plus three minus two"
 */
export function termsToSentence(terms: number[]): string {
  if (terms.length === 0) return ''
 
  const parts: string[] = [numberToEnglish(Math.abs(terms[0]))]
 
  for (let t = 1; t < terms.length; t++) {
    const term = terms[t]
    parts.push(term < 0 ? 'minus' : 'plus')
    parts.push(numberToEnglish(Math.abs(term)))
  }
 
  return parts.join(' ')
}