javascript function returns undefined when runs more than once [duplicate]

generateShipLocations returns ships if it runs once and there is no collision at first run . but when collision is true and it runs more than once , i can still see the ships inside the function and inside the else scope but it returns undefined .

generateShipLocations: function() {
    let ships = [];
    //Generates equivelant number of ships based on numships in the 
    // object and stores them in const ships array.
    for (let i = 0; i <= this.numShips - 1; i++) {
      ships.push(new model.shipObject());
    }
    // generate locations for each ship
    ships.forEach((ship) => {
      //generates ship locations
    });
    // logging the ships.it always logs.no problem here.ship locations are set correctly ; 
    console.log("ships", ships);
    //check locations if they overlap ;
    if (this.collision(ships) == true) {
      this.generateShipLocations();
    } else {
      //  i can see ships here too
      console.log("inside false if", ships)
      return ships;
    }
  },
  //collison works ok . 
  collision: function(shipObject) {
    for (let i = 0; i <= shipObject.length - 2; i++) {
      for (let j = i + 1; j <= shipObject.length - 1; j++) {
        if (condition) {
          return true;
        }
      }
    }
    return false;
  },