Home > database >  How do I correct this method
How do I correct this method

Time:08-18

So this is my code (I am learning JS). I have a method defined in my object. So for my displayCar() function I have it (what I thought) execute the method. So when it prints out the details of my generated "cars" I was expecting the method to get printed out as each car detail is printed out. But it doesn't do that. It only fires that method one time at the end of the script, no other time. So I am guessing for whatever reason, the method is not being fired, even though I thought it would be. Any insight would be greatly appreciated. Thank you

for (var i=0; i < 5; i  )
{
   var carToSell = makeCar();
   displayCar(carToSell);   
}


function makeCar()
{
  var makes = ["Chevy", "GM", "Fiat", "Ford", "Buick", "Toyota", "Nissan", "Honda", "Dodge", "Chrysler"];
  var models = ["Tahoe", "Tacoma", "4Runner", "Insight", "LeSabre", "350Z", "Passport", "Denali", "Charger", "F150"];
  var years = [1955, 2022, 1997, 1985, 1965, 1977, 2019, 2010, 2003, 1945];
  var colors = ["red", "blue", "green", "brown", "seafoam green", "yellow", "orange", "black"];
  var convertible = [true, false];
  
  var rand1 = Math.floor(Math.random() * makes.length);
  var rand2 = Math.floor(Math.random() * models.length);
  var rand3 = Math.floor(Math.random() * years.length);
  var rand4 = Math.floor(Math.random() * colors.length);
  var rand5 = Math.floor(Math.random() * 5)   1;
  var rand6 = Math.floor(Math.random() * 2);
  var rand7 = Math.floor(Math.random() * 300000);
  
  var car =
  {
    make: makes[rand1],
    model: models[rand2],
    year: years[rand3],
    color: colors[rand4],
    passengers: rand5,
    convertible: convertible[rand6],
    mileage: rand7,
    started: false,
    start: function() 
      {
        this.started = true;
      },
    stop: function() 
      {
        this.started = false;
      },
      
    drive: function() 
      {
        if (this.started)
          {
            document.write("Zoom, Zoom!!");
          }
        else
          {
            document.write("You need to start the engine first.");
          }
       }
  };
  return car;
}

function displayCar(car)
{
  document.write("Your new car is a "   car.year   " "   car.make   " "   car.model   "<br>");
  document.write("The mileage is "   car.mileage   " "   "the color is "   car.color   " and can hold "   car.passengers   " passengers.<br><br>");
  car.drive();
}

CodePudding user response:

I assume the method you are saying is not being called is drive()? If so it is getting called it's just printing on the same line as the following car information. Add some line breaks in your drive method and it will become more obvious that it is printing. Better yet make each of your printed statements a <p> tag.

See below:

for (var i=0; i < 5; i  )
{
   var carToSell = makeCar();
   displayCar(carToSell);   
}


function makeCar()
{
  var makes = ["Chevy", "GM", "Fiat", "Ford", "Buick", "Toyota", "Nissan", "Honda", "Dodge", "Chrysler"];
  var models = ["Tahoe", "Tacoma", "4Runner", "Insight", "LeSabre", "350Z", "Passport", "Denali", "Charger", "F150"];
  var years = [1955, 2022, 1997, 1985, 1965, 1977, 2019, 2010, 2003, 1945];
  var colors = ["red", "blue", "green", "brown", "seafoam green", "yellow", "orange", "black"];
  var convertible = [true, false];
  
  var rand1 = Math.floor(Math.random() * makes.length);
  var rand2 = Math.floor(Math.random() * models.length);
  var rand3 = Math.floor(Math.random() * years.length);
  var rand4 = Math.floor(Math.random() * colors.length);
  var rand5 = Math.floor(Math.random() * 5)   1;
  var rand6 = Math.floor(Math.random() * 2);
  var rand7 = Math.floor(Math.random() * 300000);
  
  var car =
  {
    make: makes[rand1],
    model: models[rand2],
    year: years[rand3],
    color: colors[rand4],
    passengers: rand5,
    convertible: convertible[rand6],
    mileage: rand7,
    started: false,
    start: function() 
      {
        this.started = true;
      },
    stop: function() 
      {
        this.started = false;
      },
      
    drive: function() 
      {
        if (this.started)
          {
            document.write("Zoom, Zoom!!");
          }
        else
          {
            document.write("<p>You need to start the engine first.</p>");
          }
       }
  };
  return car;
}

function displayCar(car)
{
  document.write("<p>Your new car is a "   car.year   " "   car.make   " "   car.model   "</p>");
  document.write("<p>The mileage is "   car.mileage   " "   "the color is "   car.color   " and can hold "   car.passengers   " passengers.</p>");
  car.drive();
}

  • Related