TheAlgorithms/JavaScript · error · Error

imageHeight should be greater than zero

Error message

imageHeight should be greater than zero

What it means

The getRGBData function requires imageHeight to be positive because it determines the number of vertical pixels. The check fires after imageWidth validation but before maxStep validation. Zero or negative height produces an empty or invalid image.

Source

Thrown at Maths/Mandelbrot.js:44

 * @param {number} maxStep Maximum number of steps to check for divergent behavior.
 * @param {boolean} useDistanceColorCoding Render in color or black and white.
 * @return {object} The RGB-data of the rendered Mandelbrot set.
 */
export function getRGBData(
  imageWidth = 800,
  imageHeight = 600,
  figureCenterX = -0.6,
  figureCenterY = 0,
  figureWidth = 3.2,
  maxStep = 50,
  useDistanceColorCoding = true
) {
  if (imageWidth <= 0) {
    throw new Error('imageWidth should be greater than zero')
  }

  if (imageHeight <= 0) {
    throw new Error('imageHeight should be greater than zero')
  }

  if (maxStep <= 0) {
    throw new Error('maxStep should be greater than zero')
  }

  const rgbData = []
  const figureHeight = (figureWidth / imageWidth) * imageHeight

  // loop through the image-coordinates
  for (let imageX = 0; imageX < imageWidth; imageX++) {
    rgbData[imageX] = []
    for (let imageY = 0; imageY < imageHeight; imageY++) {
      // determine the figure-coordinates based on the image-coordinates
      const figureX = figureCenterX + (imageX / imageWidth - 0.5) * figureWidth
      const figureY =
        figureCenterY + (imageY / imageHeight - 0.5) * figureHeight

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pass a positive integer for imageHeight.
  2. Validate all dimension parameters are > 0 before calling.
  3. Derive height from a validated width and aspect ratio.

Example fix

// before
getRGBData(800, 0)
// after
getRGBData(800, 600)
Defensive patterns

Strategy: validation

Validate before calling

if (typeof imageHeight !== 'number' || imageHeight <= 0) {
  throw new RangeError('imageHeight must be a positive number')
}
getRGBData(imageWidth, imageHeight, figureCenterX, figureCenterY, figureWidth, maxStep, useDistanceColorCoding)

Type guard

const isPositiveDimension = (d) => typeof d === 'number' && d > 0

Prevention

When it happens

Trigger: Calling getRGBData(800, 0) or getRGBData(800, -600). Any imageHeight <= 0 triggers this error (assuming imageWidth is valid).

Common situations: Height computed as zero from an aspect ratio calculation, missing config value defaulting to 0, or user-provided negative dimensions.

Related errors


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