Today's JavaScript React Dev interview Question

oathkeeper

Divyesh Parmar

Posted on August 2, 2019

Today's JavaScript React Dev interview Question

I had these questions in the interview just now

const spaceShip = {
  name: 'Chandrayan',
  get1: () => {
    console.log(this.name);
  },
  get2: function (){
    console.log(this.name);
  }
}

spaceShip.get1()
spaceShip.get2()
Enter fullscreen mode Exit fullscreen mode

now I know that spaceShip.get1() won't print out anything but if I want it to work the exact same way how the get2() works how do I bind it?

I was trying to bind this like get1.bind(spaceShip.this) or get1.bind(spaceShip) first and then execute but I'm not sure what should happen.

  • then in 2nd question
const person = this.state.person;

const person = {...this.state.person};
Enter fullscreen mode Exit fullscreen mode

what if we change the value person.name I know in the second case the value will be changes because that person is a whole new object

but in the first case will it change the value in this.state.person too?

  • I was asked about writing polling function which I didn't know but I still attempted saying
function myPoll(fn, timeInterval, endTime){

var checkCondition = function(resolve, reject) {
        var result = fn();
        if(result) {
            resolve(result);
        }
        else if (// for the time checking) {
            setTimeout(checkCondition, interval, resolve, reject);
        }
        else {
            reject(error);
        }
    };
}
Enter fullscreen mode Exit fullscreen mode

but then he dropped it.

  • 4th question was to write polyfill for Promise.all
Promise.all([pr1, pr2, pr3]).then().catch();
var resolvedPromises = [];
[pr1, pr2, pr3].map((item, resolve, reject) => {
    var result = item();
    if(result){
       resolvedPromises.push(resolve(result));
    }
    else {
        return reject(result);
    }
})


Enter fullscreen mode Exit fullscreen mode

so I tried explaining that I will store the promise in result and then push it into the array and at the end of all iteration the resolved values of all promises will be stored in that array.

But then again he asked that what if the promises doesn't get resolved or rejected than how should you tackle it so I modified the code this way

var resolvedPromises = [];
[pr1, pr2, pr3].map((item, resolve, reject) => {
   item().then((result) => {
        if(result){
            resolvedPromises.push(resolve(result));
        }
    });  //pr1;
    else {
        return reject(result);
    }
})
Enter fullscreen mode Exit fullscreen mode

then he was also confused on what to ask but that's how the interview ended, without even allowing me to ask them anything. I guess I shouldn't hope for a win today.

💖 💪 🙅 🚩
oathkeeper
Divyesh Parmar

Posted on August 2, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related