Skip to content

Instantly share code, notes, and snippets.

@nnnkit
Created May 5, 2022 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnnkit/fc9b98882f4a1a51833d7650b943ae36 to your computer and use it in GitHub Desktop.
Save nnnkit/fc9b98882f4a1a51833d7650b943ae36 to your computer and use it in GitHub Desktop.

Closure

  1. Call the function below in such a way that it returns 40.
let a = 10;
function sum(b) {
  return function (c) {
    return function (d) {
      return function (e) {
        return a + b + c + d + e;
      };
    };
  };
}

sum(10)(10)(5)(5);
// You can call with any other values the only condition is these 4 parameters total should be 30. Adding a i.e 10 will be 40.
  1. Look at the use case and complete the function callMeFiveTimes. Make sure you get the same output as given below.
function callMeFiveTimes() {
  let count = 0;
  return function(num){
    if(count < 5) {
      count++;
      return num
    } else {
      return `the function can't be called more than 5 times`;
    }
  }
}

// Using above function
let cb = callMeFiveTimes();
console.log(cb(10)); // logs 10
console.log(cb(20)); // logs 30
console.log(cb(30)); // logs 60
console.log(cb(50)); // logs 110
console.log(cb(60)); // logs 170
console.log(cb(100)); // the function can't be called more than 5 times
  1. Look at the use case and complete the function isItGreater. Make sure you get the same output as given below.
function isItGreater() {
  let first, second;
  return function(num){
    if(!first){
      first = num;
      return `This is your first value`;
    }
    if(first && !second){
      second = num;
      let message =  first > second ? `First value (${first}) is bigger than the second value (${second})` : `First value (${first}) is smaller than the second value (${second})`;
      first = null;
      second = null;
      return message;
    }
  }
}

// Using above function
let cb = isItGreater();
console.log(cb(10)); // This is your first value
console.log(cb(20)); // Second value (20) is bigger than the first value (10)
console.log(cb(30)); // This is your first value
console.log(cb(15)); // Second value (15) is smaller than the first value (30)
console.log(cb(100)); // This is your first value
console.log(cb(100)); // Second value (100) is same as the first value (100)
  1. Look at the use case and complete the function getNextElement. Make sure you get the same output as given below.
function getNextElement(array) {
  let index = 0;
  return function(){
    if(array.length === index){
      return `You have accecced all the element`;
    }
    let prevIndex = index;
    index++;
    return `The element at index ${prevIndex} is ${array[prevIndex]}`;
  }
}

// Using above function
let cb = getNextElement([10, 100, 213, 1, 12]);
console.log(cb()); // The element at index 0 is 10
console.log(cb()); // The element at index 1 is 100
console.log(cb()); // The element at index 2 is 213
console.log(cb()); // The element at index 3 is 1
console.log(cb()); // The element at index 4 is 12
console.log(cb()); // You have accecced all the element
console.log(cb()); // You have accecced all the element
  1. Create a function named oddOrEven that accepts and array of numbers. When called it returns a function. When the returned function is called it should return an odd or even number alternatively (first odd and than even).

The function will only be called until the array is empty. Once the array is empty it will return a message saying the array is empty now. Look at the example to see how it should work. Make sure to match the output with the example.

function oddOrEven(array) {
  let isOdd = true;
  return function(){
    if(array.length < 1) return `The array is empty now`
    if(isOdd){
      const index = array.findIndex(e => e % 2 !== 0);
      isOdd = false;
      if(index === -1) return `Odd number is empty now`;
      const elm = array[index];
      array.splice(index, 1);
      return `The element is ${elm} (odd)`;
      
    }else {
      const index = array.findIndex(e => e % 2 === 0);
      isOdd = true;
      if(index === -1) return `Even number is empty now`;
      const elm = array[index];
      array.splice(index, 1);
   
      return `The element is ${elm} (even)`
    }
  }
}

// Using above function
let cb = oddOrEven([10, 100, 213, 1, 12, 22]);
console.log(cb()); // The element is 213 (odd)
console.log(cb()); // The element 10 (even)
console.log(cb()); // The element 1 (odd)
console.log(cb()); // The element 100 (even)
console.log(cb()); // Odd number is empty now
console.log(cb()); // The element 12 (even)
console.log(cb()); // Even number is empty now
console.log(cb()); // The element 22 (even)
console.log(cb()); // The array is empty now
  1. Convert the code given below to use closure to protect the global variable (count) from being manipulated from any other function. (Everything can access the global variable). Everything should be wrapped inside a function
let count = 0;

function getCount() {
  return count;
}
function incrementCount() {
  count = count + 1;
  return count;
}
function decrementCount() {
  count = count + 1;
  return count;
}

// Write an example using the function you will create
function countMethods(){
  let count = 0;

  function getCount() {
    return count;
  }
  function incrementCount() {
    count = count + 1;
    return count;
  }
  function decrementCount() {
    count = count - 1;
    return count;
  }
  return {
    getCount, incrementCount, decrementCount
  }
}

let a = countMethods();
a.getCount(); // 0
a.incrementCount(); // 1
a.incrementCount(); // 2
a.incrementCount(); // 3
a.decrementCount(); // 2
a.getCount(); // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment