Interview Questions- Part-2

Currying

				
					const multiply = (a, b) => a * b;
const curriedMultiply = (a) => (b) => (c) => a * b * c;
console.log(`%c ${multiply(4)}`, "color:red;font-size:20px");
console.log(`%c ${curriedMultiply(4)(7)(2)}`, "color:red;font-size:20px");
				
			
				
					const infinitmul = (a) => {
  return (b) => {
    if (b) return infinitmul(`${a} * ${b}`);
    else {
      return a;
    }
  };
};
console.log(`%c ${infinitmul(5)(4)(8)()}`, "color:red;font-size:20px");
				
			

Functions are first class citizens

				
					//**function can be treated as variable**
const calculator = () => 4 * 3;
let x = calculator();
				
			
				
					//**function can be passed as arguments**
const printCallback = (callback) => {
  console.log(callback());
};
printCallback(calculator);
				
			
				
					//**function can return functions**
function name() {
  return function (a) {
    console.log("Hello" + a);
  };
}
				
			

Higher Order Functions vs Callback functions

  • Example 1 -HOF
  • javascriptLearning is returning function. so it is higher order function.
				
					const javascriptLearning = (name) => {
  return function (topic) {
    console.log(`Hi ${name}. Are you learning ${topic} ??`);
  };
};
javascriptLearning("Anurag")("Javascript");
//output:- Hi Anurag. Are you learning Javascript ??
				
			
  • Example 2 -HOF
  • javascriptLearning is returning function. so it is higher order function.
  • multiplication is callback function. Because this functions gets passed as an argument
  • calculator is Higher order function – which takes function as arguments. It takes operator function as argument.
				
					const addition = (x, y) => {
  return x + y;
};
const multiplication = (x, y) => {
  return x * y;
};
const calculator = (num1, num2, operator) => {
  return operator(num1, num2);
};
console.log(calculator(10, 5, multiplication))
//!OUTPUT -50
				
			

Code Refactor

				
					const sendEmail = (to, subject, body) => {
  if (to) {
    //send email
  } else {
    //some error message
  }
};
//chances of sending wrong value is more as below .
//we are sending email address in subject....
sendEmail("teaching js", "anurag.nayak@hotmail.com", "still novice");
//Refactored Code
const refactoredSendEmail = ({ to, subject, body }) => {
  if (!to) {
    //error message
  }
  //sendEmail
};
refactoredSendEmail({
  body: "improvement",
  to: "anurag.nayak@hotmail.com",
  subject: "still novice",
});
				
			

Code Refactor

& operator is O(1)

				
					let n = 64;
//Solution :-1
while (n !== 1) {
  let rem = n % 2;
  if (rem === 0) {
    n = n / 2;
    if (n === 2) {
      console.log("Number is power of 2");
      break;
    }
  } else {
    console.log("Number is not power of 2");
    break;
  }
}

let number1 = 32;
let number2 = 31;
console.log(number1.toString(2));
console.log(number2.toString(2));

//Solution :-2
let output = number2 & number1;
if (output === 0) console.log("Number is power of 2");
else console.log("Number is not power of 2");


// number1-32
// number2-31
//   100000
// * 011111
//  --------
//   000000
//  --------


// number1-34
// number2-33
//   100010
// * 100001
//  --------
//   100000
//  --------
				
			

Leave a Comment