From 1b9c17c1590111e22cdef8be437db383a8fda2fe Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Sat, 7 Nov 2026 10:46:27 +0530 Subject: [PATCH] feat: add bounce number algorithm --- Maths/BounceNumber.js | 28 ++++++++++++++++++ Maths/test/BounceNumber.test.js | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Maths/BounceNumber.js create mode 100644 Maths/test/BounceNumber.test.js diff --git a/Maths/BounceNumber.js b/Maths/BounceNumber.js new file mode 100644 index 0000000000..9a6ef75f61 --- /dev/null +++ b/Maths/BounceNumber.js @@ -0,0 +1,28 @@ +/** + * @function isBounceNumber + * @description A bounce number is a number whose digits are neither + * entirely increasing nor entirely decreasing from left to right. + * For example, 155349 is a bounce number. + * @param {number} n - A positive integer + * @returns {boolean} - True if n is a bounce number, false otherwise + * @see https://projecteuler.net/problem=112 + * @example isBounceNumber(155349) => true + * @example isBounceNumber(1234) => false + * @example isBounceNumber(9876) => false + */ +const isBounceNumber = (n) => { + if (!Number.isInteger(n) || n < 1) { + throw new TypeError('Input must be a positive integer') + } + const digits = String(n).split('').map(Number) + if (digits.length === 1) return false + let increasing = false + let decreasing = false + for (let i = 1; i < digits.length; i++) { + if (digits[i] > digits[i - 1]) increasing = true + if (digits[i] < digits[i - 1]) decreasing = true + } + return increasing && decreasing +} + +export { isBounceNumber } diff --git a/Maths/test/BounceNumber.test.js b/Maths/test/BounceNumber.test.js new file mode 100644 index 0000000000..6ced25e65f --- /dev/null +++ b/Maths/test/BounceNumber.test.js @@ -0,0 +1,51 @@ +import { isBounceNumber } from '../BounceNumber.js' + +describe('isBounceNumber', () => { + test('155349 is a bounce number', () => { + expect(isBounceNumber(155349)).toBe(true) + }) + + test('121 is a bounce number', () => { + expect(isBounceNumber(121)).toBe(true) + }) + + test('1234 is NOT a bounce number (increasing)', () => { + expect(isBounceNumber(1234)).toBe(false) + }) + + test('9876 is NOT a bounce number (decreasing)', () => { + expect(isBounceNumber(9876)).toBe(false) + }) + + test('1111 is NOT a bounce number (all same)', () => { + expect(isBounceNumber(1111)).toBe(false) + }) + + test('single digit is NOT a bounce number', () => { + expect(isBounceNumber(5)).toBe(false) + }) + + test('132 is a bounce number', () => { + expect(isBounceNumber(132)).toBe(true) + }) + + test('321 is NOT a bounce number (decreasing)', () => { + expect(isBounceNumber(321)).toBe(false) + }) + + test('123454321 is a bounce number', () => { + expect(isBounceNumber(123454321)).toBe(true) + }) + + test('throws TypeError for zero', () => { + expect(() => isBounceNumber(0)).toThrow(TypeError) + }) + + test('throws TypeError for negative input', () => { + expect(() => isBounceNumber(-121)).toThrow(TypeError) + }) + + test('throws TypeError for float input', () => { + expect(() => isBounceNumber(1.5)).toThrow(TypeError) + }) +})