📄 Table of Contents

Introduction

How to write callback functions

function createQuote(quote, callback) {
var myQuote = "A cb is run post first is , " + quote;
callback(myQuote); // 2
}
function this_is_callback_defintion(quote) {
console.log(quote);
}
createQuote("completed!", this_is_callback_defintion); // 1// A cb is run post first is completed

Polyfills for .forEach(), .map() and .filter() methods of javascript

fruits.forEach(function (element) {
console.log(element);
});
**Output:**
Mango
Apple
Banana
Grapes
Array.prototype.customForEach = function(callback){
for(i=0; i < this.length; i++){
callback(this[i])
}
}
fruits.customForEach(function(element){
console.log(element)
})

**Output:**
Mango
Apple
Banana
Grapes
const fruitsOutput = fruits.map(function(element) {
return element
})
console.log(fruitsOutput)

**Output:**
["Mango", "Apple", "Banana", "Grapes"]
Array.prototype.customMap = function(callback){
let arr = [];
for(i=0; i < this.length; i++){
arr.push(callback(this[i]))
// this = ["Mango", "Apple", "Banana", "Grapes"]
// this.length = 4;
}
return arr;
};
let fruitsOutput = fruits.customMap(function(element){
return element;
});
console.log(fruitsOutput)
**Output:**
["Mango", "Apple", "Banana", "Grapes"]
let fruits = [
{ name: 'Mango', price: 200},
{ name: 'Apple', price: 300},
{ name: 'Banana', price: 100},
{ name: 'Grapes', price: 150}
]
let fruitsFilter = fruits.filter(function (element) {
return element.price > 100;
});
console.log(fruitsFilter);

**Output:**
{name: "Mango", price: 200}
{name: "Apple", price: 300}
{name: "Grapes", price: 150}
Array.prototype.customFilter = function(callback, context){
var arr = [];
for(i=0; i < this.length; i++){
if(callback.call(context, this[i], i, this)){ // it runs with out this line as well //
arr.push(this[i])
}
}
return arr
}
let fruitsOutput = fruits.customFilter(function(element){
if(element.price > 100){
return element;
}
})
console.log(fruitsOutput)**Output:**{name: "Mango", price: 200}
{name: "Apple", price: 300}
{name: "Grapes", price: 150}

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.