Rawan
Rawan

Reputation: 11

why the input is error : error: Uncaught TypeError: Cannot read property 'indexOf' of undefined

const subwayLines = {
  Red: ['South Station', 'Park Street', 'Kendall', 'Central', 'Harvard', 'Porter', 'Davis', 'Alewife'],

  Green: ['Government Center', 'Park Street', 'Boylston', 'Arlington', 'Copley', 'Hynes', 'Kenmore'],

  Orang: ['North Station', 'Haymarket', 'Park Street', 'State', 'Downtown Crossing', 'Chinatown', 'Back Bay', 'Forest Hills']
};

const stopsBetweenStations = function(startLine, startStation, endLine, endStation) {

  startIndex = subwayLines[startLine].indexOf(startStation)

  endIndex = subwayLines[endLine].indexOf(endStation)

  if (startLine === endLine) {
    stop = Math.abs(startIndex - endIndex)

    return stop
  } else {
    startPark = subwayLines[startLine].indexOf('Park Street')

    endPark = subwayLines[endLine].indexOf('Park Street')

    stop = Math.abs(startIndex - startPark) + Math.abs(endIndex - endPark)
    return stop

  }
}

console.log(stopsBetweenStations());

the output :

Cannot read property 'indexOf' of undefined

I don't know why ): i feel it is good , it is sum value between 3 array in same object

Upvotes: 0

Views: 112

Answers (1)

mickl
mickl

Reputation: 49945

You're running your function without parameters so all of them are equal to undefined inside your function, try:

console.log(stopsBetweenStations('Red', 'Kendall', 'Orang', 'Haymarket'));

Upvotes: 2

Related Questions