{"record":{"id":"0a9b3f198506e88a","repo":"TheAlgorithms/JavaScript","slug":"number-must-be-greater-than-zero","errorCode":null,"errorMessage":"Number must be greater than zero.","messagePattern":"Number must be greater than zero\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/HexagonalNumber.js","lineNumber":18,"sourceCode":"/*\n * Author: Akshay Dubey (https://github.com/itsAkshayDubey)\n * Hexagonal Number: https://en.wikipedia.org/wiki/Hexagonal_number\n * The nth hexagonal number hn is the number of distinct dots in a pattern of dots\n * consisting of the outlines of regular hexagons with sides up to n dots, when the\n * hexagons are overlaid so that they share one vertex.\n */\n\n/**\n * @function hexagonalNumber\n * @description -> returns nth hexagonal number\n * @param {Integer} number\n * @returns {Integer} nth hexagonal number\n */\n\nexport const hexagonalNumber = (number) => {\n  if (number <= 0) {\n    throw new Error('Number must be greater than zero.')\n  }\n  return number * (2 * number - 1)\n}\n","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/HexagonalNumber.js#L1-L22","documentation":"The hexagonalNumber function computes the nth hexagonal number via the formula n*(2n-1). Hexagonal numbers are a figurate sequence defined only for n >= 1; zero and negative indices have no valid representation. The guard at line 18 rejects any number <= 0 before computing.","triggerScenarios":"Calling hexagonalNumber(0), hexagonalNumber(-1), or passing any non-positive value. A 0-based loop index fed directly into the function triggers it on the first iteration.","commonSituations":"Off-by-one loop starting at 0 instead of 1, using a 0-indexed array position as the figurate number argument, zero-defaulted variables, or user input parsed as 0.","solutions":["Pass a positive integer (n >= 1) to hexagonalNumber.","If iterating from a 0-based index, add 1 before calling: hexagonalNumber(i + 1).","Validate user input is >= 1 before invoking the function."],"exampleFix":"// before\nfor (let i = 0; i < 5; i++) {\n  hexagonalNumber(i) // throws when i === 0\n}\n// after\nfor (let i = 1; i <= 5; i++) {\n  hexagonalNumber(i)\n}","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(number) || number <= 0) {\n  throw new RangeError('number must be a positive integer')\n}\nconst result = hexagonalNumber(number)","typeGuard":"const isPositiveInteger = (n) => typeof n === 'number' && Number.isInteger(n) && n > 0","tryCatchPattern":null,"preventionTips":["Use 1-based indexing for figurate-number sequence functions.","Validate positive integers before calling number-theory or figurate-number functions.","Never pass raw 0-based loop indices to functions expecting 1-based positions."],"tags":["math","input-validation","precondition","figurate-numbers"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}