Friday, December 22, 2023

Day 17 || Call Back Function || Array Methods in JavaScript || map || filter || reduce || map vs filter vs reduce in JavaScript

JavaScript Topics

Day 17:

  • Call Back Function
  • Array Methods
    • Map(),
    • Filter(),
    • Reduce()

Call Back Function:

function name(x) {
  console.log("Hema");
  role();
}

function role() {
  console.log("Student");
}

name(role);

let name = (x)=> {
    console.log("Hema");
    role();
  }
 
  let  role = ()=> {
    console.log("Student");
  }
 
  name(role);


Array Methods:

let originalArray = [1, 2, 3, 4, 5];

let newArray = originalArray.map((currValue, ind, arr) => {

//   console.log(currValue * 2);
    return currValue % 2 === 0
});

console.log(newArray);

let originalArray = [1, 2, 3, 4, 5];
let multiply = {
  number: 2,
};

let newArray = originalArray.map(function(currValue, ind, arr) {
//   console.log(currValue * this.number);
  return currValue * this.number;
}, multiply);

console.log(newArray);
let originalArray = [1, 2, 3, 4, 5];

let filterArray = originalArray.filter((currValue, ind, arr) => {
  return currValue % 2 === 0;
});

console.log(filterArray);

let originalArray = [6, 1, 2, 3, 4, 9, 5, 6, 8, 7];

let reduceArray = originalArray.reduce((acc, currValue, ind, arr) => {
    return acc + currValue;
});

console.log(reduceArray);


let originalArray = [1, 2, 3, 4, 5];

let newArray = originalArray.map((currValue, ind, arr) => currValue * 3);
console.log(newArray);

let evenNumber = newArray.filter((currValue) => currValue % 2 === 0);
console.log(evenNumber);

let singleValue = evenNumber.reduce((acc, currValue) => acc + currValue);
console.log(singleValue);
let newArray = originalArray
  .map((currValue, ind, arr) => currValue * 3)
  .filter((currValue) => currValue % 2 === 0)
  .reduce((acc, currValue) => acc + currValue);
console.log(newArray);


Tasks:

Task 1: Call Back
Write a function calculate that takes two numbers and a callback function as arguments. The callback function should perform a mathematical operation (addition, subtraction, multiplication, or division) on the two numbers.

Task 2: Map + Filter
Given an array of words, create a new array that contains the lengths of words that have more than three characters.
const words = ['apple', 'banana', 'pear', 'grape', 'kiwi'];

Task 3: Map + Reduce
Given an array of numbers, create a new array that contains the square of each number and then calculate the sum of the squared values.
const numbers = [1, 2, 3, 4, 5];

Interview Questions:

1. Why are callback functions useful in JavaScript?
2. What is the difference between using map and a for loop for iterating over an array?
3. How does the filter method differ from the map method?
4. Explain the concept of the reduce method in JavaScript?






No comments:

Post a Comment

Top 10 | JavaScript Coding Interview Question | Beginner Level

               JavaScript Coding Interview  Q. No. 01/10:  console . log ( "1" + "4" + "1" ) // 141 console . ...