{"record":{"id":"b89a9365ada971d9","repo":"TheAlgorithms/JavaScript","slug":"argument-must-be-an-array","errorCode":null,"errorMessage":"Argument must be an Array","messagePattern":"Argument must be an Array","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/MeanSquareError.js","lineNumber":5,"sourceCode":"// Wikipedia: https://en.wikipedia.org/wiki/Mean_squared_error\n\nconst meanSquaredError = (predicted, expected) => {\n  if (!Array.isArray(predicted) || !Array.isArray(expected)) {\n    throw new TypeError('Argument must be an Array')\n  }\n\n  if (predicted.length !== expected.length) {\n    throw new TypeError('The two lists must be of equal length')\n  }\n\n  let err = 0\n\n  for (let i = 0; i < expected.length; i++) {\n    err += (expected[i] - predicted[i]) ** 2\n  }\n\n  return err / expected.length\n}\n\nexport { meanSquaredError }\n","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/MeanSquareError.js#L1-L22","documentation":"The meanSquaredError function computes MSE between predicted and expected value arrays via element-wise iteration. Both arguments must be arrays; the guard checks this before the length-equality check. Non-array arguments trigger this error.","triggerScenarios":"Calling meanSquaredError(\"123\", [1,2,3]), meanSquaredError([1,2], null), or passing a single number instead of an array for either argument.","commonSituations":"Passing a single prediction instead of an array of predictions, JSON deserialization type mismatches, or variables that were expected to be arrays but are undefined.","solutions":["Ensure both arguments are arrays.","Wrap single values in an array if comparing only one data point.","Validate with Array.isArray() for both arguments before calling."],"exampleFix":"// before\nmeanSquaredError(42, expected)\n// after\nmeanSquaredError([42], expected)","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(predicted) || !Array.isArray(expected)) {\n  throw new TypeError('Both arguments must be arrays')\n}\nmeanSquaredError(predicted, expected)","typeGuard":"const areBothArrays = (a, b) => Array.isArray(a) && Array.isArray(b)","tryCatchPattern":null,"preventionTips":["Wrap single prediction values in an array before calling.","Validate both arguments with Array.isArray() before invoking.","Check that JSON-deserialized data is actually an array, not an object or primitive."],"tags":["math","statistics","type-check","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}