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

Added Advanced Javascript programs #984

Merged
merged 1 commit into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function makeRequest(location) {
return new Promise((resolve, reject) => {
console.log(`Making Request to ${location}`);

if (location === "Google") {
resolve("Google says Hi..");
} else {
reject("We only talk with Google");
}
});
}

function processRequest(response) {
return new Promise((resolve, reject) => {
console.log("processing response");
resolve(`Extra information ${response}`);
});
}

// makeRequest("Google")
// .then((response) => {
// console.log("Response has been recieved");
// return processRequest(response);
// })
// .then((processResponse) => {
// console.log(processResponse);
// })
// .catch((err) => {
// console.log(err);
// });

async function doWork() {
try {
const response = await makeRequest("Facebook");
console.log("response recieved");
const processedResponse = await processRequest(response);
console.log(processedResponse);
} catch (err) {
console.log(err);
}
}

doWork();
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//if statements

const x = "20";

if (x === 20) {
console.log(true);
} else {
console.log(false);
}

//ternary operators

const y = 20;

const color = y <= 20 ? "red" : "blue";
console.log(color);

//switch case statements

switch (color) {
case "red":
console.log("color is red");
break;
case "blue":
console.log("colore is blue");
break;
default:
console.log("color is not red or blue");
break;
}

//functions

function addNumber(num1, num2) {
console.log(num1 + num2);
}

addNumber(10, 12);

//fat arrow function

const addNumbers = (num3, num4) => {
console.log(num3 + num4);
};
addNumbers(100, 89);

//object oriented programming

function Person(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = new Date(dob);
}

// Instantiate object
const Person1 = new Person("Akash", "kumar", "2-2-2001");

console.log(Person1);
// alert(Person1.firstName + " " + Person1.lastName + " " + Person1.dob);

//classes

class Person_Class {
constructor(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = new Date(dob);
}
getBirthYear() {
return this.dob.getFullYear();
}

getFullName() {
return `${this.firstName} ${this.lastName}`;
}
}

const Person_Class_1 = new Person_Class("Harry", "Potter", "4-2-1992");

console.log(Person_Class_1.getFullName());
console.log(Person_Class_1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const companies = [
{ name: "company one", category: "Finanace", start: 1981, end: 2003 },
{ name: "company two", category: "Retail", start: 1989, end: 2043 },
{ name: "company three", category: "Auto", start: 1451, end: 2053 },
{ name: "company four", category: "Crypto", start: 1681, end: 2603 },
{ name: "company five", category: "Factories", start: 1933, end: 2503 },
{ name: "company six", category: "Machines", start: 1931, end: 2203 },
{ name: "company seven", category: "Food", start: 1541, end: 2033 },
{ name: "company eight", category: "Clothes", start: 1581, end: 2093 },
{ name: "company nine", category: "Agritech", start: 1781, end: 2083 },
{ name: "company ten", category: "Cyber-security", start: 1581, end: 2883 },
{ name: "company eleven", category: "Clocks", start: 1986, end: 2803 },
{ name: "company twelve", category: "Oil", start: 1951, end: 2088 },
{ name: "company thirten", category: "Natural Gas", start: 1881, end: 2803 },
{ name: "company fourteen", category: "Retail", start: 1901, end: 2603 },
{ name: "company fifteen", category: "Food", start: 1281, end: 2303 },
];

const ages = [22, 23, 44, 45, 66, 71, 34, 54, 65, 34, 56, 29, 78, 95, 21];

//for loop

// for (let i = 0; i < companies.length; i++) {
// console.log(companies[i]);
// }

//forEach loop

// companies.forEach((company) => {
// console.log(company);
// });

// map function

// companies.map((company) => {
// console.log(company);
// });

//filter method

//1. age

// const canDrink = ages.filter((age) => {
// if (age >= 40) {
// return true;
// }
// });

// console.log(canDrink);

//2. companies

// const retailCompanies = companies.filter((company) => {
// if (company.category === "Retail" || company.category === "Food") {
// return true;
// } else if (company.start >= 1500 && company.start <= 2093) {
// return true;
// }
// });

// console.log(retailCompanies);

//map

//create array ofcompany names

// const companyName = companies.map((company) => {
// return `${company.category} [${company.start} - ${company.end}]`;
// });
// console.log(companyName);

let students = [
{ id: "001", name: "Anish", sports: "Cricket" },
{ id: "002", name: "Smriti", sports: "Basketball" },
{ id: "003", name: "Rahul", sports: "Cricket" },
{ id: "004", name: "Bakul", sports: "Basketball" },
{ id: "005", name: "Nikita", sports: "Hockey" },
];

let players = students
.filter((student) => {
return student.sports === "Cricket";
})
.map((student) => {
return student.name;
});

console.log("Cricekt player is " + players);
//The below code does the same thing except it iterates players function one more time and gives result in different lines.

// players.map((player) => {
// console.log("Cricket player is " + player);
// });

//sort

const sortCompanies = companies.sort((c1, c2) => {
if (c1.start < c2.start) {
return 1;
} else {
return -1;
}
});

console.log(sortCompanies);

//sort ages

const sortAges = ages.sort((a, b) => {
return b - a; //in decending order
});
console.log(sortAges);

//reduce

const ageSum = ages.reduce((total, age) => {
return total + age;
}, 0);

console.log(ageSum);
94 changes: 94 additions & 0 deletions Program's_Contributed_By_Contributors/JavaScript_Programs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const names =
"Akash Kumar , Roshan kumar , Tarun singh , Harneet Kaur , Deepak , Abhi";

const age = 20;

console.log(names.split(", "));

//Arrays

const numbers = new Array(1, 2, 3, 3, 4, 4, 5, 5, 5, 5);

console.log(numbers);

const Names = ["Akash", "kumar", "sharma", 20, "student"];
Names.push("Lovely professional university");
Names.unshift("Mr.");

console.log(Names);

console.log(Names.indexOf("kumar"));

console.log(Names[0] + " " + Names[1]);

//Object literals

const Person = {
firstName: "Akash",
middleName: "kumar",
lastName: "sharma",
age: 20,
hobbies: ["music", "football", "cricket", "programming", "movies"],
college: "Lovely proffessional university",
address: {
street: "Ih 131 jct mill thapar colony",
city: "Phagwara",
state: "Punjab",
country: "India",
pinCode: 144401,
},
};

Person.email = "[email protected]";
console.log(Person);

console.log(Person.address.pinCode);

//making Todos

const todos = [
{ id: 1, text: "Learn Javascript", isComplete: true },
{ id: 2, text: "Learn React", isComplete: false },
{ id: 3, text: "Learn Node.js", isComplete: false },
];

const todoJSON = JSON.stringify(todos);

console.log(todoJSON);

//for Loop

for (let i = 0; i < todos.length; i++) {
console.log(todos[i].text);
}

for (let todo of todos) {
console.log(todo.id, todo.text, todo.isComplete);
}

//higher order functions

//1. for each
todos.forEach((todo) => {
console.log(todo.text);
});

//2. map

const todoText = todos.map((todo) => {
return todo.text;
});

console.log(todoText);

//3. filter

const todoCompleted = todos
.filter((todo) => {
return todo.isComplete === true;
})
.map((todo) => {
return todo.text;
});

alert(todoCompleted);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let p = new Promise((resolve, reject) => {
let a = 1 + 7;
if (a === 2) {
resolve("Hello world");
} else {
reject("Failed");
}
});

p.then((message) => {
console.log(message);
}).catch((err) => {
console.log(err);
});
Loading