diff --git a/event.html b/event.html new file mode 100644 index 0000000..7017f91 --- /dev/null +++ b/event.html @@ -0,0 +1,63 @@ + + + + + + JavaScript Events Example + + + + + + + + + + diff --git a/firstclass.html b/firstclass.html new file mode 100644 index 0000000..08f8c3d --- /dev/null +++ b/firstclass.html @@ -0,0 +1,50 @@ + + + + + +First-Class Functions Example + + + +

First-Class Functions Example

+ +

Result:

+ + + + + diff --git a/oop with inheritance.js b/oop with inheritance.js new file mode 100644 index 0000000..7f4837c --- /dev/null +++ b/oop with inheritance.js @@ -0,0 +1,32 @@ + +class Animal { + constructor(name) { + this.name = name; + } + + speak() { + console.log(`${this.name} makes a sound.`); + } +} + +class Dog extends Animal { + constructor(name, breed) { + super(name); + this.breed = breed; + } + + speak() { + console.log(`${this.name} barks.`); + } + + fetch() { + console.log(`${this.name} fetches a ball.`); + } +} + +const animal = new Animal('Generic Animal'); +const dog = new Dog('Buddy', 'Labrador'); + +animal.speak(); +dog.speak(); +dog.fetch(); diff --git a/oop.js b/oop.js new file mode 100644 index 0000000..e1741aa --- /dev/null +++ b/oop.js @@ -0,0 +1,96 @@ +class LivingBeing { + constructor(name) { + this._name = name; + } + + get name() { + return this._name; + } + + set name(newName) { + if (typeof newName === 'string') { + this._name = newName; + } else { + throw new Error('Name must be a string.'); + } + } + + static breath() { + console.log('Taking a breath...'); + } +} + +class Animal extends LivingBeing { + constructor(name) { + super(name); + } + + speak() { + console.log(`${this.name} makes a sound.`); + } +} + +class Dog extends Animal { + constructor(name, breed) { + super(name); + this._breed = breed; + } + + get breed() { + return this._breed; + } + + set breed(newBreed) { + if (typeof newBreed === 'string') { + this._breed = newBreed; + } else { + throw new Error('Breed must be a string.'); + } + } + + speak() { + console.log(`${this.name} barks.`); + } + + fetch() { + console.log(`${this.name} fetches a ball.`); + } +} + +class WorkingDog extends Dog { + constructor(name, breed, task) { + super(name, breed); + this._task = task; + } + + get task() { + return this._task; + } + + set task(newTask) { + if (typeof newTask === 'string') { + this._task = newTask; + } else { + throw new Error('Task must be a string.'); + } + } + + performTask() { + console.log(`${this.name} is ${this.task}.`); + } +} + +const livingBeing = new LivingBeing('Organism'); +const animal = new Animal('Generic Animal'); +const dog = new Dog('Buddy', 'Labrador'); +const workingDog = new WorkingDog('Rex', 'German Shepherd', 'herding'); + +LivingBeing.breath(); + +livingBeing.name = 'New Organism'; +console.log(livingBeing.name); + +animal.speak(); +dog.speak(); +dog.fetch(); +workingDog.performTask(); diff --git a/program1.js b/program1.js new file mode 100644 index 0000000..71ad9fe --- /dev/null +++ b/program1.js @@ -0,0 +1,12 @@ +//Create a function calculateSquare that calculates the square of a number. Use a prototype to reuse the function across instances. + +function Calculator(base) { + this.base = base; +} +Calculator.prototype.calculateSquare = function() { + return this.base * this.base; +}; +var calc1 = new Calculator(10); +var calc2 = new Calculator(20); +console.log(calc1.calculateSquare()); // Outputs: 25 +console.log(calc2.calculateSquare()); // Outputs: 64 \ No newline at end of file diff --git a/program2.js b/program2.js new file mode 100644 index 0000000..dd811d7 --- /dev/null +++ b/program2.js @@ -0,0 +1,31 @@ +// Online Javascript Editor for free +// Write, Edit and Run your Javascript code using JS Online Compiler +// Creating a Prototype with Inhertiance and Adding a Method to the Prototype + +function Person(name,age,grade){ + this.name=name; + this.age=age; + +} + +Person.prototype.school='City School' + +Person.prototype.sayHello=function(){ + console.log(`My name is ${this.name} and my age is ${this.age}`) +} + +const Person1= new Person('Charlie',20,10) +Person1.sayHello() + +function Student(name, age, grade) { + Person.call(this, name, age); + this.grade = grade; +} +Student.prototype = Object.create(Person.prototype); + +Student.prototype.checkGrade = function() { + console.log(`${this.name} is studying in ${this.school} and he got ${this.grade} grade!`); +}; + +const student1 = new Student("Charlie", 22, "A"); +student1.checkGrade() diff --git a/program3.js b/program3.js new file mode 100644 index 0000000..b49cd5e --- /dev/null +++ b/program3.js @@ -0,0 +1,27 @@ +// Description: Explore the prototype chain of an object. Create instances of Person, Student (inheriting from Person), and School. + +function Person(name,age,grade){ + this.name=name; + this.age=age; + +} +function Student(name, age, grade) { + Person.call(this, name, age); + this.grade = grade; +} +Student.prototype = Object.create(Person.prototype); + +function Graduate(name, age, grade, school) { + Student.call(this, name, age, grade); + this.school = school; +} +Graduate.prototype = Object.create(Student.prototype); +var person = new Person("Thomas", 10); +var student = new Student("Doe", 12, "A"); +var graduate = new Graduate("Alpha", 13, "B", "Becon"); +console.log(graduate.name); +console.log(graduate.grade); +console.log(graduate.school); +console.log(student.name); +console.log(student.grade); +console.log(student.school); \ No newline at end of file diff --git a/program4.js b/program4.js new file mode 100644 index 0000000..b66fc57 --- /dev/null +++ b/program4.js @@ -0,0 +1,73 @@ + +// Add and remove books and search by author and type +function Book(title, author, type) { + this.title = title; + this.author = author; + this.type = type; + this.available = true; +} + +function Library() { + this.books = []; + this.checkedOutBooks = []; +} + +Library.prototype.addBook = function(book) { + this.books.push(book); +}; + +Library.prototype.removeBook = function(type) { + this.books = this.books.filter(book => book.type !== type); +}; + +Library.prototype.findBookByType = function(type) { + return this.books.find(book => book.type === type); +}; + +Library.prototype.checkOutBook = function(type) { + const book = this.findBookByType(type); + if (book && book.available) { + book.available = false; + this.checkedOutBooks.push(book); + console.log(`Book "${book.title}" by ${book.author} checked out successfully.`); + } else { + console.log("Book not available or does not exist."); + } +}; + +Library.prototype.returnBook = function(type) { + const index = this.checkedOutBooks.findIndex(book => book.type === type); + if (index !== -1) { + const returnedBook = this.checkedOutBooks.splice(index, 1)[0]; + returnedBook.available = true; + console.log(`Book "${returnedBook.title}" by ${returnedBook.author} returned successfully.`); + } else { + console.log("Book not found in checked out list."); + } +}; + +Library.prototype.listCheckedOutBooks = function() { + console.log("Checked out books:"); + this.checkedOutBooks.forEach(book => { + console.log(`- ${book.title} by ${book.author}`); + }); +}; + +Library.prototype.searchBooks = function(keyword) { + const foundBooks = this.books.filter(book => book.title.includes(keyword) || book.author.includes(keyword)); + console.log("Search results:"); + foundBooks.forEach(book => { + console.log(`- ${book.title} by ${book.author}`); + }); +}; + +// Usage example +var library = new Library(); +var book1 = new Book("Title 1", "Author 1", "Fiction"); +var book2 = new Book("Title 2", "Author 2", "Real"); +library.addBook(book1); +library.addBook(book2); +library.checkOutBook("Fiction"); +library.listCheckedOutBooks(); +library.returnBook("Fiction"); +library.searchBooks("Author 2");