TheAlgorithms/JavaScript · error · TypeError

The ${noName} should be Number type

Error message

The ${noName} should be Number type

What it means

Thrown by the shared `isNumber(number, noName)` helper used by every volume function (e.g. `volHemisphere(radius)`). It fires the TypeError branch when `typeof number !== 'number'`. The placeholder `${noName}` is filled with a label like 'Radius', 'Height', or the default 'number', identifying which argument was wrong.

Source

Thrown at Maths/Volume.js:117

*/
const volSphere = (radius) => {
  isNumber(radius, 'Radius')
  return (4 / 3) * Math.PI * radius ** 3
}

/*
  Calculate the volume for a Hemisphere
  Reference: https://www.cuemath.com/measurement/volume-of-hemisphere/
  return (2 * PI * radius^3)/3
*/
const volHemisphere = (radius) => {
  isNumber(radius, 'Radius')
  return (2.0 * Math.PI * radius ** 3) / 3.0
}

const isNumber = (number, noName = 'number') => {
  if (typeof number !== 'number') {
    throw new TypeError('The ' + noName + ' should be Number type')
  } else if (number < 0 || !Number.isFinite(number)) {
    throw new Error('The ' + noName + ' only accepts positive values')
  }
}

export {
  volCuboid,
  volCube,
  volCone,
  volPyramid,
  volCylinder,
  volTriangularPrism,
  volPentagonalPrism,
  volSphere,
  volHemisphere
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce each dimension with `Number(...)` and verify `typeof === 'number'` upstream.
  2. For string sources, `parseFloat` and confirm `Number.isFinite`.
  3. Check every argument, not just the first - the label tells you which one failed.

Example fix

// before
volHemisphere(formData.radius) // string '5'

// after
const r = Number(formData.radius)
if (typeof r !== 'number' || !Number.isFinite(r)) throw new TypeError('radius must be a number')
volHemisphere(r)
Defensive patterns

Strategy: type-guard

Validate before calling

const dims = [length, width, height].map(Number)
if (!dims.every((d) => typeof d === 'number' && Number.isFinite(d))) {
  throw new TypeError('all dimensions must be finite numbers')
}
volCuboid(...dims)

Type guard

const isNumber = (x) => typeof x === 'number'

Prevention

When it happens

Trigger: Call `volHemisphere('5')`, `volCuboid(null, 2, 3)`, `volSphere(undefined)`, `volCone(BigInt(3), 4)`. Any non-number argument to any `vol*` function triggers it with the appropriate label.

Common situations: Form or query-string inputs that arrive as strings, missing object fields passed via destructuring, or mixed BigInt/Number arithmetic from a parser.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/9bcd4cae1c206218. Report an issue: GitHub.