From e257b7915b60259087a05a435bf151c6eeafb559 Mon Sep 17 00:00:00 2001 From: patrickwestervelt Date: Sat, 22 Oct 2022 10:27:33 -0400 Subject: [PATCH] Algorithm: FindLcm improvement --- Maths/FindLcm.js | 23 ++++++++++++++++++++--- Maths/test/FindLcm.test.js | 29 ++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/Maths/FindLcm.js b/Maths/FindLcm.js index 101da8565a..c20c867a48 100644 --- a/Maths/FindLcm.js +++ b/Maths/FindLcm.js @@ -11,16 +11,18 @@ 'use strict' +import { findHCF } from './findHcf' + // Find the LCM of two numbers. const findLcm = (num1, num2) => { // If the input numbers are less than 1 return an error message. if (num1 < 1 || num2 < 1) { - return 'Please enter values greater than zero.' + throw Error('Numbers must be positive.') } // If the input numbers are not integers return an error message. if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) { - return 'Please enter whole numbers.' + throw Error('Numbers must be whole.') } // Get the larger number between the two @@ -33,4 +35,19 @@ const findLcm = (num1, num2) => { } } -export { findLcm } +// Typically, but not always, more efficient +const findLcmWithHcf = (num1, num2) => { + // If the input numbers are less than 1 return an error message. + if (num1 < 1 || num2 < 1) { + throw Error('Numbers must be positive.') + } + + // If the input numbers are not integers return an error message. + if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) { + throw Error('Numbers must be whole.') + } + + return num1 * num2 / findHCF(num1, num2) +} + +export { findLcm, findLcmWithHcf } diff --git a/Maths/test/FindLcm.test.js b/Maths/test/FindLcm.test.js index 1e5c0905cd..0a744cf5ae 100644 --- a/Maths/test/FindLcm.test.js +++ b/Maths/test/FindLcm.test.js @@ -1,20 +1,39 @@ -import { findLcm } from '../FindLcm' +import { findLcm, findLcmWithHcf } from '../FindLcm' describe('findLcm', () => { it('should throw a statement for values less than 1', () => { - expect(findLcm(0, 0)).toBe('Please enter values greater than zero.') + expect(() => { findLcm(0, 0) }).toThrow(Error) }) it('should throw a statement for one value less than 1', () => { - expect(findLcm(1, 0)).toBe('Please enter values greater than zero.') - expect(findLcm(0, 1)).toBe('Please enter values greater than zero.') + expect(() => { findLcm(1, 0) }).toThrow(Error) + expect(() => { findLcm(0, 1) }).toThrow(Error) }) it('should return an error for values non-integer values', () => { - expect(findLcm(4.564, 7.39)).toBe('Please enter whole numbers.') + expect(() => { findLcm(4.564, 7.39) }).toThrow(Error) }) it('should return the LCM of two given integers', () => { expect(findLcm(27, 36)).toBe(108) }) }) + +describe('findLcmWithHcf', () => { + it('should throw a statement for values less than 1', () => { + expect(() => { findLcmWithHcf(0, 0) }).toThrow(Error) + }) + + it('should throw a statement for one value less than 1', () => { + expect(() => { findLcmWithHcf(1, 0) }).toThrow(Error) + expect(() => { findLcmWithHcf(0, 1) }).toThrow(Error) + }) + + it('should return an error for values non-integer values', () => { + expect(() => { findLcmWithHcf(4.564, 7.39) }).toThrow(Error) + }) + + it('should return the LCM of two given integers', () => { + expect(findLcmWithHcf(27, 36)).toBe(108) + }) +}) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy