Skip to main content

Day5 - Functions

Published 3 min readMarkdown

https://courses.chaicode.com/learn/home/30-days-of-Javascript-challenge/30-days-javascript-challenge/section/515627/lesson/3197105

Screenshot:-

Functions are fundamental building blocks in JavaScript, allowing you to encapsulate code for reuse and better organization. In this article, we will explore various ways to define and use functions, including function declarations, function expressions, arrow functions, function parameters with default values, and higher-order functions.

Activity 1: Function Declaration

Task 1: Check if a Number is Even or Odd

function checkEvenorOdd(num) {
  if (num % 2 === 0) {
    console.log("Even");
  } else {
    console.log("Odd");
  }
}

checkEvenorOdd(3); // Output: Odd

This function checks if a given number is even or odd and logs the result to the console.

Task 2: Calculate the Square of a Number

function squareofNumber(num) {
  console.log("Square of", num, "is", num * num);
}

squareofNumber(5); // Output: Square of 5 is 25

This function calculates the square of a number and logs the result.

Activity 2: Function Expression

Task 3: Find the Maximum of Two Numbers

function maxNumber(num1, num2) {
  if (num1 > num2) {
    console.log("num1 is the maximum number.");
  } else {
    console.log("num2 is the maximum number.");
  }
}

maxNumber(2, 5); // Output: num2 is the maximum number.

This function finds the maximum of two numbers and logs the result.

Task 4: Concatenate Two Strings

function concatstrings(str1, str2) {
  console.log(str1 + str2);
}

concatstrings("I am", " Alpit."); // Output: I am Alpit.

This function concatenates two strings and logs the result.

Activity 3: Arrow Functions

Task 5: Calculate the Sum of Two Numbers

const calculatesum = (num1, num2) => {
  console.log(num1 + num2);
};

calculatesum(2, 3); // Output: 5

This arrow function calculates the sum of two numbers and logs the result.

Task 6: Check if a String Contains a Specific Character

const checkchar = (str, chr) => {
  return str.includes(chr);
};

console.log(checkchar("Alpit", "A")); // Output: true

This arrow function checks if a string contains a specific character and returns a boolean value.

Activity 4: Function Parameters and Default Values

Task 7: Return the Product of Two Parameters

const product = (num1, num2 = 2) => {
  console.log(num1 * num2);
};

product(5, 5); // Output: 25
product(5); // Output: 10

This arrow function takes two parameters and returns their product, with a default value for the second parameter.

Activity 5: Higher-Order Functions

Task 8: Call a Function Multiple Times

const callfunctionmanytimes = (fn, times) => {
  for (let i = 0; i < times; i++) {
    fn();
  }
};

const printHello = () => {
  console.log("Hello");
};

callfunctionmanytimes(printHello, 5);
// Output:
// Hello
// Hello
// Hello
// Hello
// Hello

This higher-order function takes a function and a number and calls the function that many times.

Task 9: Apply Two Functions Sequentially

const applymultipletimes = (fn1, fn2, val) => {
  console.log(fn2(fn1(val)));
};

const add1 = (num) => num + 1;
const square = (num) => num * num;

applymultipletimes(add1, square, 5); // Output: 36

This higher-order function takes two functions and a value, applies the first function to the value, and then applies the second function to the result.

About the author

  • I am a passionate web developer and open-source enthusiast on a captivating journey of coding wonders. With a year of experience in web development, my curiosity led me to the enchanting world of React, where I found a true calling. Embracing the magic of collaboration and knowledge-sharing, I ventured into the realm of open source, contributing to Digital Public Goods (DPGs) for the betterment of the digital universe. A firm believer in learning in public, I share my insights and discoveries through blogging, inspiring fellow coders to embark on their own magical coding odysseys. Join me on this thrilling adventure, where imagination and technology converge, and together, let's shape the future of the digital landscape! 🎩✨

No comments yet. Be the first to comment.

More from this blog