Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
daniyalrathore14 authored Jun 6, 2024
0 parents commit a5cfaa9
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 0 deletions.
63 changes: 63 additions & 0 deletions event.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Events Example</title>
</head>
<body>

<input type="text" id="myInput" placeholder="Type here">
<button id="myButton">Click Me</button>

<script>
var input = document.getElementById('myInput');
var button = document.getElementById('myButton');

function handleInputMouseDown() {
sendMessage("Mouse button pressed down over the input field!");
}

function handleInputMouseUp() {
sendMessage("Mouse button released over the input field!");
}

function handleInputMouseOver() {
sendMessage("Mouse pointer entered the input field!");
}

function handleInputMouseOut() {
sendMessage("Mouse pointer left the input field!");
}

function handleButtonClick() {
sendMessage("Button clicked!");
}

function handleButtonMouseOver() {
sendMessage("Mouse pointer entered the button!");
}

function handleButtonMouseOut() {
sendMessage("Mouse pointer left the button!");
}

// input event listeners
input.addEventListener('mousedown', handleInputMouseDown);
input.addEventListener('mouseup', handleInputMouseUp);
input.addEventListener('mouseover', handleInputMouseOver);
input.addEventListener('mouseout', handleInputMouseOut);

// button event listeners
button.addEventListener('click', handleButtonClick);
button.addEventListener('mouseover', handleButtonMouseOver);
button.addEventListener('mouseout', handleButtonMouseOut);

// Function to send a message
function sendMessage(message) {
console.log(message);
}
</script>

</body>
</html>
50 changes: 50 additions & 0 deletions firstclass.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>First-Class Functions Example</title>
</head>
<body>

<h2>First-Class Functions Example</h2>

<p>Result: <span id="result"></span></p>

<script>
function operate(func, x, y) {
return func(x, y);
}

function add(a, b) {
return a + b;
}

function subtract(a, b) {
return a - b;
}

function multiply(a, b) {
return a * b;
}

function divide(a, b) {
return a / b;
}

document.getElementById("result").innerText = operate(add, 5, 3); // Output: 8

const myFunc = function(x) {
return x * x;
};

const functionsArray = [add, subtract, multiply, divide];
console.log(functionsArray[0](8, 2));
console.log(functionsArray[1](8, 2));
console.log(functionsArray[2](8, 2));
console.log(functionsArray[3](8, 2));

</script>

</body>
</html>
32 changes: 32 additions & 0 deletions oop with inheritance.js
Original file line number Diff line number Diff line change
@@ -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();
96 changes: 96 additions & 0 deletions oop.js
Original file line number Diff line number Diff line change
@@ -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();
12 changes: 12 additions & 0 deletions program1.js
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions program2.js
Original file line number Diff line number Diff line change
@@ -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()
27 changes: 27 additions & 0 deletions program3.js
Original file line number Diff line number Diff line change
@@ -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);
73 changes: 73 additions & 0 deletions program4.js
Original file line number Diff line number Diff line change
@@ -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");

0 comments on commit a5cfaa9

Please sign in to comment.