Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relearning javascript #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Marcus0224
Copy link

No description provided.

@Marcus0224
Copy link
Author

bug fixes

@@ -6,3 +6,15 @@
* - Create several objects using the class.
* - Test the objecs by calling their properties and using their methods in the console.
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this example, I'll use a Car class that has properties like make, model, year, and methods like startEngine and stopEngine.

Car Class Definition

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
    this.isEngineOn = false;
  }

  startEngine() {
    if (!this.isEngineOn) {
      this.isEngineOn = true;
      console.log(`${this.make} ${this.model} engine started.`);
    } else {
      console.log(`${this.make} ${this.model} engine is already on.`);
    }
  }

  stopEngine() {
    if (this.isEngineOn) {
      this.isEngineOn = false;
      console.log(`${this.make} ${this.model} engine stopped.`);
    } else {
      console.log(`${this.make} ${this.model} engine is already off.`);
    }
  }
}

Creating Objects and Testing

Now, let's create several objects using the Car class and test them by accessing their properties and using their methods.

// Creating car objects
const car1 = new Car("Toyota", "Corolla", 2020);
const car2 = new Car("Honda", "Civic", 2019);
const car3 = new Car("Ford", "Mustang", 2021);

// Accessing properties
console.log(`${car1.make} ${car1.model} (${car1.year})`);
console.log(`${car2.make} ${car2.model} (${car2.year})`);
console.log(`${car3.make} ${car3.model} (${car3.year})`);

// Testing methods
car1.startEngine();
car1.stopEngine();

car2.startEngine();
car2.startEngine(); // Trying to start the engine again
car2.stopEngine();

car3.startEngine();
car3.stopEngine();
car3.stopEngine(); // Trying to stop the engine again

// Output for each method call is printed to the console

Explanation:

  1. Class Definition:

    • The Car class has a constructor that initializes the make, model, year, and isEngineOn properties.
    • The startEngine method checks if the engine is off and starts it if true, otherwise, it indicates that the engine is already on.
    • The stopEngine method checks if the engine is on and stops it if true, otherwise, it indicates that the engine is already off.
  2. Creating Objects:

    • We create three instances (car1, car2, car3) of the Car class, each representing a different car.
  3. Accessing Properties:

    • We print out the make, model, and year properties of each car object to the console.
  4. Testing Methods:

    • We test the startEngine and stopEngine methods on each car object. The console output will show the engine status and handle cases where the engine is already in the desired state.

This demonstrates the basics of object-oriented programming in JavaScript using a class to create and manage objects, as well as how to work with their properties and methods.

@@ -7,3 +7,21 @@
* - Find an object that has another object inside of it to create a nested object.
* - Test your objects in the browser console by accessing the entire object and its specific properties.
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const house = {
year: "1973",
make: "Duplex",
color: "Cream",
model: {
Side1: "two bedroom two bath",
Side2: "one bedroom one bath"
},
houseOpen: false,
newModel: function(Right, Left) {
this.model.Side1 = Right;
this.model.Side2 = Left;
}
};

// Accessing the entire object and specific properties
console.log(house);
console.log("Year:", house.year);
console.log("Make:", house.make);
console.log("Color:", house.color);
console.log("Model (before):", house.model);
console.log("Model Side1:", house.model.Side1);
console.log("Model Side2:", house.model.Side2);

// Using the newModel method to update the model
house.newModel("three bedroom three bath", "two bedroom one bath");

// Checking the updates
console.log("Model (after):", house.model);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants