{"record":{"id":"c1e588df9a27ec9e","repo":"TheAlgorithms/JavaScript","slug":"rule-must-be-an-integer-between-the-values-0-and-2","errorCode":null,"errorMessage":"Rule must be an integer between the values 0 and 255 (got ${rule})","messagePattern":"Rule must be an integer between the values 0 and 255 \\(got (.+?)\\)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Cellular-Automata/Elementary.js","lineNumber":73,"sourceCode":" * 000000001101010101010101010101010101010101011000000      000000001100110000000000000000000000000110011000000\n * 000000011101010101010101010101010101010101011100000      000000011111111000000000000000000000001111111100000\n *\n * DEV NOTE: This implementation assumes that cells on the edge (who only have 1 neighbor) have 1 neighbor and a permanently \"dead\" neighbor, which is technically correct in a finite space. However, most diagrams of these elementary cellular automata rules assume a infinite line of cells. Therefore, the edges of the array may not evolve perfectly in line with pictured diagrams which assume infinite space.\n */\n\n/**\n * Find the next Elementary Cell Automata Generation given the previous generation and the rule [0-255] to follow\n * @param {(0 | 1)[]} generation The current generation of the Elementary Cellular Automata simulation\n * @param {number} rule The current rule of the Elementary Cellular Automata simulation. Must be an integer between 0 and 255 inclusive\n * @returns {(0 | 1)[]} The next generation according to the inputted rule\n */\nexport function getNextElementaryGeneration(generation, rule) {\n  const NUM_ELEMENTARY_NEIGHBORHOOD_STATES = 8\n  const MIN_RULE = 0\n  const MAX_RULE = 255\n\n  if (!Number.isInteger(rule)) {\n    throw new Error(\n      `Rule must be an integer between the values 0 and 255 (got ${rule})`\n    )\n  }\n  if (rule < MIN_RULE || rule > MAX_RULE) {\n    throw new RangeError(\n      `Rule must be an integer between the values 0 and 255 (got ${rule})`\n    )\n  }\n\n  const binaryRule = rule\n    .toString(2)\n    .padStart(NUM_ELEMENTARY_NEIGHBORHOOD_STATES, '0')\n  const ruleData = binaryRule.split('').map((bit) => Number.parseInt(bit)) // note that ruleData[0] represents \"all alive\" while ruleData[7] represents \"all dead\"\n  const output = new Array(generation.length)\n  const LEFT_DEAD = 4 // 100 in binary\n  const MIDDLE_DEAD = 2 // 010 in binary\n  const RIGHT_DEAD = 1 // 001 in binary\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Cellular-Automata/Elementary.js#L55-L91","documentation":"Thrown by getNextElementaryGeneration when the rule is not an integer. Elementary cellular automata rules are defined by an 8-bit lookup table indexed 0..255, so a fractional/string/NaN rule cannot map to a deterministic rule table. This is the type guard (Error); the range check at line 78 is separate.","triggerScenarios":"Passing a float (e.g. 30.5), a numeric string (\"30\"), NaN, undefined, or any value failing Number.isInteger.","commonSituations":"Reading the rule from user input or a URL query param (always a string), or applying a parseFloat that introduced a fraction.","solutions":["Pass an integer literal: getNextElementaryGeneration(gen, 30).","Parse strings explicitly: Math.trunc(Number(raw)) and confirm it is an integer.","Guard untrusted input with Number.isInteger(rule) before calling."],"exampleFix":"// before\ngetNextElementaryGeneration(gen, params.rule) // string from query\n// after\nconst rule = Math.trunc(Number(params.rule))\nif (Number.isInteger(rule)) getNextElementaryGeneration(gen, rule)","handlingStrategy":"type-guard","validationCode":"function nextGenSafe(gen, rule) {\n  const r = Math.trunc(Number(rule));\n  if (!Number.isInteger(r)) throw new Error('Rule must be an integer');\n  return getNextElementaryGeneration(gen, r);\n}","typeGuard":"/** @param {unknown} r @returns {r is number} */\nconst isIntRule = r => Number.isInteger(r);","tryCatchPattern":"try { return getNextElementaryGeneration(gen, rule); }\ncatch (e) {\n  if (/integer between the values 0 and 255/.test(e.message) && !Number.isInteger(rule)) {\n    return getNextElementaryGeneration(gen, Math.trunc(Number(rule)));\n  }\n  throw e;\n}","preventionTips":["Parse rule strings with Number()/parseInt before calling.","Use Number.isInteger to guard query-param/argv input.","Keep the type check and range check distinct so errors are diagnosable."],"tags":["cellular-automata","rule-validation","type-validation","integer"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}