avatarKirti K

Summary

The website content provides a comprehensive guide for mastering JavaScript basics, focusing on the concept of pure functions, recursion, and interview preparation through practical examples and explanations.

Abstract

The provided web content is part of a "100 Days of JavaScript Interview Prep: A Beginner’s Journey" series, specifically for Day 13. It delves into advanced topics such as pure functions, which are defined as functions that consistently return the same result for the same inputs without side effects, and recursive functions, which are functions that call themselves. The article uses examples like an add function and a square function to illustrate pure functions, and it demonstrates a recursive function with a factorial example. The author, Kirti Kaushal, emphasizes the importance of understanding these concepts for interview preparation and effective programming. The content also outlines the advantages of pure functions, such as ease of testing and debugging, and the benefits and drawbacks of recursion. The article concludes with an encouragement for readers to share knowledge, be kind, and support the author's work by subscribing and following.

Opinions

  • The author believes that embarking on a 100-day learning journey is rewarding and focuses on growth through practice.
  • Pure functions are highly regarded for their testability, debuggability, and alignment with functional programming principles.
  • Recursive functions are presented as a powerful tool for breaking down complex problems, despite their potential to consume significant memory resources.
  • The author advocates for the importance of recursive thinking in programming.
  • Kindness is highlighted as a core value, with the author urging readers to be respectful and supportive in their interactions.
  • The author values community engagement and knowledge sharing as means to gain knowledge.

JavaScript Day 13 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

Welcome to Day 13–100 Days of JavaScript Interview Prep: A Beginner’s Journey

Welcome to Day 13–100 Days of JavaScript Interview Prep: A Beginner’s Journey

Embarking on a 100-day adventure focused on learning, practice, and growth sounds like an incredibly rewarding journey.

DAY 13 (April 4th, 2024) — Continuing Functions in JavaScript with examples

Question 1- What is the concept of pure functions with example JavaScript?

Answer :

A pure function is a function that always returns the same result given the same inputs .

It has no side effects and doesn’t modify any external variables or state, and its output only depends on its inputs.

function add(a, b) {
  return a + b;
}

In this example, the add function takes two parameters, a and b, and returns their sum.

This function is pure because it doesn't modify any external variables or state, and its output only depends on its inputs.

Another example

function square(x) {
  return x * x;
}

The square function takes a single parameter x and returns its square.

Consider now Impure function to understand it better.

let total = 0;

function addToTotal(x) {
  total += x;
  return total;
}

This function addToTotal modifies the external variable total every time it's called, which means its behavior is dependent on and modifies external state.

This makes it impure because its output not only depends on its input but also on the current state of the total variable.

Advantages :

Easy to test, debug and reason about.

They facilitate functional programming paradigms such as immutability and referential transparency

Question 2 : Give an example when a function called itself?

Similar question — Create a recursive function (factorial)

Answer : Can a function called itself ? Yes, it can

This concept is known as recursion, and functions that call themselves are called recursive functions.

function factorial(n) {
  // Base case: if n is 0 or 1, return 1
  if (n === 0 || n === 1) {
    return 1;
  }
  // Recursive case: n * factorial(n - 1)
  return n * factorial(n - 1);
}

// Calculate factorial of 5
console.log(factorial(5)); // Output: 120

Important Tip : Recursive thinking is really important in programming.

Advantages

It helps you break down bit problems into smaller ones.

Disadvantages

It can consume a lot of memory.

Happy Coding and Stay Tuned :)

Kind is cool !!!

Share knowledge to gain knowledge !!!!

Be kind and treat people with respect !!

If you find my videos helpful, then please subscribe, Claps and follow to support :)

Cheers

Kirti Kaushal

Other Articles

Coding Interviews
Javascript Development
Javascript Tips
Beginner Coding
Programming
Recommended from ReadMedium