SDE1🏢 Zeta Practice Problem

Create a sampling function

Problem Description & Specifications

This question was asked in ZETA's SDE1 Frontend interview.

Create a function in JavaScript that accepts a function as input and a count and executes that input function once for a given count of calls. Known as sampling function.

function message(){
  console.log("hello");
}

const sample = sampler(message, 4);
sample();
sample();
sample();
sample(); // this will be executed
sample();
sample();
sample();
sample(); // this will be executed
Note: You have to implement a function sampler that evaluates the above expression. There are no test cases to this. Test it yourself with different values.

Related Links
- https://www.linkedin.com/posts/prakharchauhan281098_opentowork-frontenddevelopment-javascript-share-7281324741347430400-6sx1/?utm_source=share&utm_medium=member_desktop

Implementation & Test Harness

Editable Monaco Runner
JavaScript Lab Sandbox
/*
This question was asked in ZETA's SDE1 Frontend interview.

Create a function in JavaScript that accepts a function as input and a count and executes that input function once for a given count of calls. Known as sampling function.

function message(){
  console.log("hello");
}

const sample = sampler(message, 4);
sample();
sample();
sample();
sample(); // this will be executed
sample();
sample();
sample();
sample(); // this will be executed
Note: You have to implement a function sampler that evaluates the above expression. There are no test cases to this. Test it yourself with different values.

Related Links
- https://www.linkedin.com/posts/prakharchauhan281098_opentowork-frontenddevelopment-javascript-share-7281324741347430400-6sx1/?utm_source=share&utm_medium=member_desktop
*/

function samplingFunction(func, count, context) {
  let counter = 0;
  return function (...args) {
    context = this ?? context;
    if (++counter !== count) return;
    func.apply(context, args);
    counter = 0;
  }
}

function message() {
  console.log('Hello');
};

const sample = samplingFunction(message, 4);

sample()
sample()
sample()
sample()
sample()
Console Output
Click "Run Code" above to execute and view output...