Master the Javascript Interview: Functional Programming
This is designed to prepare you for common interview questions.
Photo by Austin Chan on Unsplash
This is a Hottt topic in JS world. What is this? Why is this? And How is this? We will see this all along with some popular practice Questions more like a challenge for you.
One thing about the interview is, It Looks scarier than IT IS.
In this chapter, we will be going through FP(Functional Programming). So Let's say you got to know about FP somewhere, but were unable to understand, you googled saw some videos and still, you are not able to understand or code FP.
That's mostly because functional programming is nothing but a way of writing code, which makes it easier and more understandable. but if you are unfamiliar with the concept give it some time and these things will get easier with time. So let me provide you with some concepts that will help you :
- Pure Functions
- Immutability
- Higher-Order Functions
- Imperative
Still Confused?
Solve this to check your understanding.
Let's understand each concept one by one.
Pure Function
- a pure function is a function that has the following properties: the function return values are identical for identical arguments, and the function application has no side effects. Thus a pure function is a computational analog of a mathematical function. *
Pure functions are functions that give the same output for the same input. They have three simple rules.
- They return at least one argument.
- They return a function or a value.
- It should not mutate any of its arguments.
Functions in JS are "first-class citizens", that is we can work with functions the same as variables. One of the key things in functional programming is pure functions which makes it beautiful, and easy to understand and test. One of the reasons that this is so loved among developers is that it makes the testing real simple.
//Write a function MyStockProfits()
// to take the company name and a number in an object
// and multiply it with 10.
const Reliance = { Name: "Reliance industries", Value : 2700}
const MyStockProfits = (Reliance)=> ({...Reliance, Value : Reliance.Value*10})
//to check this
MyStockProfits(Reliance);
//output
{Name: 'Reliance industries', Value: 27000}
Immutability
A very important concept in functional programming is immutability. This means that objects, once created cannot be modified. It is simple yet powerful when we design big programs and we would just have one thing to change. Let us understand with an example.
const stockPortfolio = {
Company : "Reliance",
Value : 2700
};
const addStock = stockPortfolio;
addStock.Company = "JGK";
Higher Order Functions
They are a great way used to abstract and write better code. Those functions which take a function as a parameter is known as a higher-order function. Examples can be array.map(), array.filter, and array. reduce(They are the popular kids in the world of JS)
const number = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const add10toNumber = number.map(num => num+10);
//output
[11, 12, 13, 14, 15, 16, 17, 18, 19]
Imperative vs Declarative
Declarative and Imperative programming are two different Programming paradigms that help us achieve different good things. (They look like two opposite twins to me). Imperative is all about how to get a specific result whereas Declarative is all about What to do .
//Imperative Programming(AAAAAM ZINDAGI..!)
const numbers = [11, 21, 31, 41, 51];
const sumNumbers = (n) => {
let Result = 0;
for (let i = 0; i < n.length; i++) {
finalResult += n[i];
}
return Result;
}
//Declarative Programming(Mentos Zindagi....!!!!!!)
const numbers = [11, 21, 31, 41, 51];
const sumNumber = (n) =>
n.reduce((acc, curr) => acc + curr);
Functional Programming prefers declarative programming more than imperative.
Smart are those who learn from others and their mistakes.
Words of Wisdom
If you have come here, you know that all these concepts are nothing but just the theory and your brain cells will forget all of these in just a matter of time. If you really want to know and understand this topic, Google: Reduce, filter, and map Practice Questions. They are the crux of functional programming.
Good day!
Shavi