UNPKG

nhb-toolbox

Version:

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

46 lines (45 loc) 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._find2NumbersLCM = exports._find2NumbersHCF = exports._applyMultiples = void 0; exports._convertLessThanThousand = _convertLessThanThousand; const constants_1 = require("./constants"); const _applyMultiples = (array, multiples) => { if (!multiples) return array; return array?.filter((n) => n % multiples === 0); }; exports._applyMultiples = _applyMultiples; function _convertLessThanThousand(num, isLast) { if (num < 10) return constants_1.ONES[num]; if (num < 20) return constants_1.TEENS[num - 10]; let result = constants_1.TENS[Math.floor(num / 10)]; const remainder = num % 10; if (remainder > 0) result += `-${constants_1.ONES[remainder]}`; if (num >= 100) { const hundredsPart = `${constants_1.ONES[Math.floor(num / 100)]} hundred`; return num % 100 === 0 ? hundredsPart : `${hundredsPart} ${isLast ? 'and' : ''} ${_convertLessThanThousand(num % 100, false)}`; } return result; } 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; }; exports._find2NumbersHCF = _find2NumbersHCF; const _find2NumbersLCM = (a, b) => { const x = Math.abs(a); const y = Math.abs(b); return (x * y) / (0, exports._find2NumbersHCF)(x, y); }; exports._find2NumbersLCM = _find2NumbersLCM;