Skip to content

Instantly share code, notes, and snippets.

View suhailgupta03's full-sized avatar
:octocat:

Suhail Gupta suhailgupta03

:octocat:
View GitHub Profile
// SINGLE RESPONSIBILITY PRINCIPLE
class User {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
let cache = {}; // just a simple js object
// cache is just some space in RAM (memory)
function getStudentDetailsFromDatabase(name) { // AWS - US east
return {
name: `${name} is a student`,
age: Math.floor(Math.random() * 100)
} // response time is n seconds
}
function main(name) {
if(cache[name]) {
let cache = {}; // just a simple js object
// cache is just some space in RAM (memory)
function getStudentDetailsFromDatabase() { // AWS - US east
return {
name: 'John',
} // response time is n seconds
}
function main() {
if(cache["result"]) {
return cache["result"];
db.collection.aggregate([
{ $unwind: "$id" },
{ $group: {
_id: "$id",
titles: { $push: "$title" }
}},
{ $group: {
_id: null,
distinctDocuments: { $push: { id: "$_id", title: { $first: "$titles" } } }
}},
function sum(a, b) {
return a + b;
}
function divide(a, b) {
return a / b;
}
function calculatePercentage(a, b) {
return divide(a, b) * 100;
let person = {
firstName: "Vikash",
lastName: "Sagar",
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
console.log(person.fullName()); // Output: Vikash Sagar
let person1 = {
firstName: "John",
// Problem: Given a instagram post, we need to identify "all" the hashtags in the post
// Assumption: Hashtags will only have alphanumeric characters
// alphanumeric: [a-zA-Z0-9]
function extractHashTag(post) {
let pattern = /#[a-zA-Z0-9]+/g
// [a-zA-Z0-9] means any character from a to z, A to Z, 0 to 9
// + means one or more
const matchResult = post.match(pattern); // ["#beachday", "#day1"]
return matchResult;
let string = "Enjoying my day at the beach! #beachday #day1";
let pattern = /\w/g
// \w means any word character (equal to [a-zA-Z0-9_])
// which means that \w includes underscore
// anything between a-z
// anything between A-Z
// anything between 0-9
console.log(string.match(pattern));
let patternNumber = /\d/g
// \d means any digit (equal to [0-9])
let string = "Anish is a student with alma better. So is Subrojyoti";
let pattern = /[a-z]/g
// [a-z] means any character from a to z
// [A-Z] means any character from A to Z
// [0-9] means any character from 0 to 9
console.log(string.match(pattern));
let stringNew = "My age is 23. I was born in 1997";
let patternNew = /[0-9]/g
// [0-9] is a range that matches any character from 0 to 9
let string = `Hi! Recently India celebrated its Independence Day`
// 1. Find all 'e' and replace it with 'E'
// 2. Replace the 'Independence Dat' in the sentence with 'Republic Day'
// 3. Find the index of 'India' in the string
let string2 = "hello world how are you?";
// 1. Find all the spaces in the string