{"record":{"id":"4c8572e2ab68a6ec","repo":"TheAlgorithms/JavaScript","slug":"the-two-lists-must-be-of-equal-length","errorCode":null,"errorMessage":"The two lists must be of equal length","messagePattern":"The two lists must be of equal length","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/MeanSquareError.js","lineNumber":9,"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 requires element-wise comparison, so predicted and expected arrays must have the same length. This guard fires after the array-type check. A length mismatch would cause the loop to silently skip elements or access undefined values.","triggerScenarios":"Calling meanSquaredError([1,2,3], [1,2]) or any pair of arrays with differing lengths.","commonSituations":"Truncated datasets, filtering that removed elements from one array but not the other, train/test split mismatches, or predictions generated for a different number of samples than ground truth.","solutions":["Ensure both arrays have the same number of elements.","Trim or pad arrays to equal length before calling.","Check .length equality before invoking."],"exampleFix":"// before\nmeanSquaredError(pred, actual) // pred.length !== actual.length\n// after\nif (pred.length !== actual.length) throw new RangeError('arrays must match in length')\nmeanSquaredError(pred, actual)","handlingStrategy":"validation","validationCode":"if (predicted.length !== expected.length) {\n  throw new RangeError('Arrays must have equal length')\n}\nmeanSquaredError(predicted, expected)","typeGuard":"const areEqualLengthArrays = (a, b) => Array.isArray(a) && Array.isArray(b) && a.length === b.length","tryCatchPattern":null,"preventionTips":["Check .length equality before pairwise comparison operations.","Trim or pad datasets to matching sizes before calling.","Audit filtering and train/test splits for asymmetry that changes array lengths."],"tags":["math","statistics","input-validation","ml"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}