{"record":{"id":"e302af1f96f0c73d","repo":"TheAlgorithms/JavaScript","slug":"invalid-input","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/AverageMean.js","lineNumber":14,"sourceCode":"/**\n * @function mean\n * @description This script will find the mean value of a array of numbers.\n * @param {number[]} numbers - Array of integer\n * @return {number} - mean of numbers.\n * @throws {TypeError} If the input is not an array or contains non-number elements.\n * @throws {Error} If the input array is empty.\n * @see [Mean](https://en.wikipedia.org/wiki/Mean)\n * @example mean([1, 2, 4, 5]) = 3\n * @example mean([10, 40, 100, 20]) = 42.5\n */\nconst mean = (numbers) => {\n  if (!Array.isArray(numbers)) {\n    throw new TypeError('Invalid Input')\n  } else if (numbers.length === 0) {\n    throw new Error('Array is empty')\n  }\n\n  let total = 0\n  numbers.forEach((num) => {\n    if (typeof num !== 'number') {\n      throw new TypeError('Invalid Input')\n    }\n    total += num\n  })\n\n  return total / numbers.length\n}\n\nexport { mean }\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/AverageMean.js#L1-L31","documentation":"Thrown by mean(numbers) (AverageMean.js:14) as a TypeError when the argument is not an array (Array.isArray returns false). This is the first of three validation checks in mean: non-array throws here, empty array throws separately, and non-number elements throw inside the loop. The function expects a single array argument, not spread/rest arguments.","triggerScenarios":"Call mean(1, 2, 3) (passing spread args instead of an array); mean('123') passing a string; mean({length: 3}) passing an array-like object; mean(42) passing a scalar; mean(null) from a missing field.","commonSituations":"Confusing mean([1,2,3]) with mean(1,2,3) — the function is not variadic; passing a Set or Map that must be spread to an array first; passing arguments object from a legacy function; receiving undefined from a failed JSON lookup.","solutions":["Wrap the arguments in an array literal: mean([1, 2, 3]) not mean(1, 2, 3).","Convert array-likes with Array.from() or spread: mean([...mySet]).","Guard the call: if (!Array.isArray(x)) return NaN or convert.","If the value may be undefined from optional chaining, default it: mean(data ?? [])."],"exampleFix":"// before\nconst m = mean(...values) // spreads into separate args, not an array\n\n// after\nconst m = mean(Array.isArray(values) ? values : [values])\n// or simply:\nconst m = mean(values) // pass the array directly","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(data)) {\n  data = Array.from(data) // for iterables, or wrap single values\n}\nconst m = mean(data)","typeGuard":"const isNumberArray = (v) => Array.isArray(v) && v.every(x => typeof x === 'number')","tryCatchPattern":"try {\n  m = mean(numbers)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Invalid Input') {\n    // not an array — convert and retry\n  } else throw e\n}","preventionTips":["mean takes a single array argument, not spread args — never call mean(1,2,3).","Convert Sets/Maps with Array.from before passing.","Default optional inputs with ?? [] so undefined never reaches the function."],"tags":["type-check","array","input-validation","statistics"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}