TheAlgorithms/JavaScript · error · TypeError

Argument is not a string.

Error message

Argument is not a string.

What it means

Thrown by RailwayTimeConversion when timeString is not of type 'string'. It is the first and only type guard; once it passes the function immediately calls timeString.split(':') and .substring, which would otherwise throw a less clear error. TypeError.

Source

Thrown at Conversions/RailwayTimeConversion.js:21

    because we know that if the time is in 'AM' value it means they only want
    some changes on hours and minutes and if the time in 'PM' it means the only
    want some changes in hour value.

    Input Format -> 07:05:45PM
    Output Format -> 19:05:45

    Problem & Explanation Source : https://www.mathsisfun.com/time.html
*/

/**
 * RailwayTimeConversion method converts normalized time string to Railway time string.
 * @param {String} timeString Normalized time string.
 * @returns {String} Railway time string.
 */
const RailwayTimeConversion = (timeString) => {
  // firstly, check that input is a string or not.
  if (typeof timeString !== 'string') {
    throw new TypeError('Argument is not a string.')
  }
  // split the string by ':' character.
  const [hour, minute, secondWithShift] = timeString.split(':')
  // split second and shift value.
  const [second, shift] = [
    secondWithShift.substring(0, 2),
    secondWithShift.substring(2)
  ]
  // convert shifted time to not-shift time(Railway time) by using the above explanation.
  if (shift === 'PM') {
    if (parseInt(hour) === 12) {
      return `${hour}:${minute}:${second}`
    } else {
      return `${parseInt(hour) + 12}:${minute}:${second}`
    }
  } else {
    if (parseInt(hour) === 12) {
      return `00:${minute}:${second}`

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Format the input as a string in 'hh:mm:ssAM|PM' shape before calling (e.g. '07:05:45PM').
  2. If you have a Date, format it yourself rather than passing the Date object.
  3. Type-check with typeof timeString === 'string' upstream.

Example fix

// before
RailwayTimeConversion(someDateObject)
// after
const d = someDateObject
const fmt = `${String(d.getHours() % 12 || 12).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}:${String(d.getSeconds()).padStart(2, '0')}${d.getHours() >= 12 ? 'PM' : 'AM'}`
RailwayTimeConversion(fmt)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof timeString !== 'string') {
  throw new TypeError('timeString must be a string like 07:05:45PM')
}
RailwayTimeConversion(timeString)

Type guard

const isString = (x) => typeof x === 'string'

Try / catch

try {
  RailwayTimeConversion(timeString)
} catch (e) {
  if (e instanceof TypeError && /not a string/.test(e.message)) {
    return RailwayTimeConversion(String(timeString))
  }
  throw e
}

Prevention

When it happens

Trigger: Calling RailwayTimeConversion(70545), RailwayTimeConversion(null), or RailwayTimeConversion(['07','05','45']). After this guard, malformed-but-string inputs like '7:5' will not throw here — they will produce odd output instead, so the guard only covers the type.

Common situations: Passing a Date object instead of a formatted string; numeric time from a picker; undefined from an optional field; an array from a split done upstream.

Related errors


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