{"record":{"id":"c5cd6ab456d875ce","repo":"TheAlgorithms/JavaScript","slug":"expected-integer-n-and-finite-a-b-c5cd6a","errorCode":null,"errorMessage":"Expected integer N and finite a, b","messagePattern":"Expected integer N and finite a, b","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/SimpsonIntegration.js","lineNumber":30,"sourceCode":" * We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:\n * I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}\n *\n * That means that the first and last indexed i f(xi) are multiplied by 1,\n * the odd indexed f(xi) by 4 and the even by 2.\n *\n * N must be even number and a<b. By increasing N, we also increase precision\n *\n * More info: [Wikipedia link](https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule)\n *\n */\n\nfunction integralEvaluation(N, a, b, func) {\n  // Check if N is an even integer\n  let isNEven = true\n  if (N % 2 !== 0) isNEven = false\n\n  if (!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) {\n    throw new TypeError('Expected integer N and finite a, b')\n  }\n  if (!isNEven) {\n    throw Error('N is not an even number')\n  }\n  if (N <= 0) {\n    throw Error('N has to be >= 2')\n  }\n\n  // Check if a < b\n  if (a > b) {\n    throw Error('a must be less or equal than b')\n  }\n  if (a === b) return 0\n\n  // Calculate the step h\n  const h = (b - a) / N\n\n  // Find interpolation points","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/SimpsonIntegration.js#L12-L48","documentation":"The integralEvaluation function approximates a definite integral using composite Simpson's rule, which requires N to be an even integer (Simpson's rule needs pairs of intervals). The guard checks for integer N and non-NaN a/b. Note: the code computes isNEven before the type check, so odd integers pass this guard and hit a separate 'N is not an even number' error; this specific error is for non-integer N or NaN a/b.","triggerScenarios":"Calling integralEvaluation(10.5, 0, 1, f) (non-integer N), integralEvaluation(10, NaN, 1, f) (NaN a), or integralEvaluation(\"10\", 0, 1, f) (string N). Odd integer N like 11 does NOT trigger this error (it triggers the even-check instead).","commonSituations":"N derived from a non-integer division, NaN from missing interval bounds, or string values from configuration files.","solutions":["Ensure N is an even integer.","Round N with Math.round() and adjust to the nearest even number.","Validate a and b are finite numbers before calling."],"exampleFix":"// before\nintegralEvaluation((b - a) / h, a, b, f)\n// after\nconst N = Math.round((b - a) / h)\nintegralEvaluation(N % 2 !== 0 ? N + 1 : N, a, b, f)","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(N) || N % 2 !== 0 || !Number.isFinite(a) || !Number.isFinite(b)) {\n  throw new TypeError('Expected even integer N and finite a, b')\n}\nintegralEvaluation(N, a, b, func)","typeGuard":"const isValidSimpsonInput = (N, a, b) =>\n  Number.isInteger(N) && N % 2 === 0 && Number.isFinite(a) && Number.isFinite(b)","tryCatchPattern":null,"preventionTips":["Ensure N is an even integer for Simpson's rule by rounding up to the nearest even number.","Validate interval bounds a and b are finite before calling.","Round N with Math.round() and adjust parity (N + 1 if odd) before invoking."],"tags":["math","calculus","type-validation","numerical-integration"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}