UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

39 lines (38 loc) 1.12 kB
import { ONES, TEENS, TENS } from './constants.js'; export const _applyMultiples = (array, multiples) => { if (!multiples) return array; return array?.filter((n) => n % multiples === 0); }; export function _convertLessThanThousand(num, isLast) { if (num < 10) return ONES[num]; if (num < 20) return TEENS[num - 10]; let result = TENS[Math.floor(num / 10)]; const remainder = num % 10; if (remainder > 0) result += `-${ONES[remainder]}`; if (num >= 100) { const hundredsPart = `${ONES[Math.floor(num / 100)]} hundred`; return num % 100 === 0 ? hundredsPart : `${hundredsPart} ${isLast ? 'and' : ''} ${_convertLessThanThousand(num % 100, false)}`; } return result; } export const _find2NumbersHCF = (a, b) => { let x = Math.abs(a); let y = Math.abs(b); while (y !== 0) { const temp = y; y = x % y; x = temp; } return x; }; export const _find2NumbersLCM = (a, b) => { const x = Math.abs(a); const y = Math.abs(b); return (x * y) / _find2NumbersHCF(x, y); };