// IIFE - immediately invoked function expression(()=>{console.log("I got executed immediately");})();((name: string,age: number)=>{console.log(name,age);})("Nabin",19);// async(async()=>{returnnewPromise((resolve,reject)=>{setTimeout(()=>{resolve("I got executed immediately");},1000);});})();// add two number((add: (x: number,y: number)=>number)=>{console.log(add(1,2));// 3})((x: number,y: number)=>x+y);// ((add: Function) => {// console.log(add(1, 2)); // 3// })((x: number, y: number) => x + y);// counter example using iife((counter: {count: number;next: ()=>number})=>{console.log(counter.count);// 0console.log(counter.next());// 1;})({count: 0,next(){return++this.count;},});((Person)=>{constperson=newPerson("Nabin");console.log(person.sayHello());// "Hello, my name is Alice"})(class{constructor(publicname: string){}sayHello(){return`Hello, my name is ${this.name}`;}});export{};```tsconstnumbers: number[]=[1,2,3,4,5];constiterator=numbers[Symbol.iterator]();console.log(iterator.next());// { value: 1, done: false }console.log(iterator.next());// { value: 2, done: false }console.log(iterator.next());// { value: 3, done: false }console.log(iterator.next());// { value: 4, done: false }console.log(iterator.next());// { value: 5, done: false }console.log(iterator.next());// { value: undefined, done: true }
Symbol
// symbol are not created by new keyword// const symbol1 = Symbol()// symbol have description// Symbol('username')// symbol are uniqueconstsymbol1=Symbol("user");constsymbol2=Symbol("user");console.log(typeofsymbol1===typeofsymbol2);// false
Check common string
// export function twoString(s1: string, s2: string): "yes" | "no" {// let sMap: Record<string, boolean> = {};// let hasCommonString = false;// for (let i = 0; i < s1.length; i++) {// for (let j = 0; j < s2.length; j++) {// if (s1[i] === s2[j]) {// hasCommonString = true;// sMap[s1[i]] = true;// sMap[s2[j]] = true;// }// }// }// return hasCommonString ? "yes" : "no";// }// export function twoStringexportfunctiontwoString(s1: string,s2: string): "yes"|"no"{letsMap: Record<string,boolean>={c: true,o: true,d: true,e: true,};lethasCommonString: boolean=false;[...s1].forEach((text: string)=>{if(!text){sMap[text]=true;}});for(leti=0;i<s2.length;i++){if(sMap[s2[i]]){hasCommonString=true;}}returnhasCommonString ? "yes" : "no";}
Fizbuzz but slightly different way
// show the fizz buzz inside the arrayfunctionfizzbuzz(n: number): string[]{return[...Array(n)].map((_,index)=>{// return [...Array(n).keys(n)].map((index) => {constnums=index+1;console.log(nums);if(nums%2==0&&nums%3==5)return"Fizzbuzz";if(nums%3==0)return"buzz";if(nums%2==0)return"fizz";returnnums.toString();});}
Generic way of getting random dice roll in TS
exportfunctionrollDice(arr: any[]): number{returnMath.floor(Math.random()*arr.length);}functiongetRandom<T,S>(arr1: T[],arr2: S[]): [T,S]{consta=arr1[rollDice(arr1)];constb=arr2[rollDice(arr2)];console.log(a);return[a,b];}constfruits: string[]=["apple","banana","grapes","mango","orange"];constdiceNumbers: number[]=[1,2,3,4,5,6];exportconstlog=getRandom(fruits,diceNumbers);// random string and random number// function (arr:any[]):number// output - [1,'grapes']constrollDiceRandom=(arr: any[]): number=>{returnMath.floor(Math.random()*arr.length);};constgetRandomDiceAndOther=<T,S>(dice: T[],fruit: S[]): [T,S]=>{constd=dice[rollDiceRandom(dice)];constf=fruit[rollDiceRandom(fruit)];return[d,f];};getRandomDiceAndOther(fruits,diceNumbers);// ['grapes',3] // might be whatever just my case