DEV Community

Divyesh Parmar
Divyesh Parmar

Posted on

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.

Top comments (9)

Collapse
 
kepta profile image
Kushan Joshi • Edited

The Promise.all question kept me up at night, here is my attempt:

async function promiseAll(promises) {
  return new Promise((resolve, reject) => {
    const result = []; // create an array
    const resolver = (p) => (val) => {
      result[promises.indexOf(p)] = val; // fill it directly, I know this is suboptimal as it creates an array with holes
      // Object.keys helps with finding actual length (without holes) of the array
      if (Object.keys(result).length === promises.length) {
        resolve(Array.from(result)); // Array.from converts the sub-optimal array with holes to a regular array
      }
    };
    promises.forEach((p) => p.then(resolver(p), reject)); // reject early to match Promise.all's early rejection behaviour
  });
}

The most important thing here is:

  • to early reject, just like Promise.all
  • Keep the order of array
Collapse
 
oathkeeper profile image
Divyesh Parmar

Wow this is complicated, I need to focus :D

 
oathkeeper profile image
Divyesh Parmar

Oh yes you I should have directly gone with forEach after that. As I said I was still explaining and talking my approach but I do understand this is a test and I'm not supposed to make it a debate haha! :)

Thread Thread
 
admwx7 profile image
Austin Murdock

It's possible he stopped you mid working on the question for time constraints, I've had to do that quite a few times as interviews are pretty tight on time.

On the first question, spaceship.get1 = spaceship.get1.bind(spaceships) should work, though the easy answer is "don't use an arrow function to ensure proper this context" and it likely would of been accepted as the underlying question there is asking if you know how the this context works. I would give extra points if you just removed the : and => from the example.

Overall on the questions, I've run interviews like this in the past. The goal is to see how familiar you are with the more challenging concepts in their code base. I would guess they need someone now and don't have the time or resources needed to train someone on the concepts they use most frequently. This is surprisingly common and leaves out a lot of devs with strong potential that just haven't been exposed to the concepts in question.

In the case where I see an interviewee struggling on a topic I follow one of two paths:
1) if I know enough to gauge their technical skills then I use the remaining time to try and teach them the concepts so they get to learn while I get to see how well they take feedback and learn, which is a more useful skill in the long run
2) if I don't have enough information I move to a different question, this rarely happens though as I try to form my questions as extremely basic and build upon them to start with so I can also get a gauge for code extensibility and to avoid stumping people early on before I get enough information

Thread Thread
 
oathkeeper profile image
Divyesh Parmar

It's possible he stopped you mid working on the question for time constraints

Hmm I understand this totally, but he seem so much confused more than me and stuttred a lot while asking and all.

On the first question, spaceship.get1 = spaceship.get1.bind(spaceships) should work,

thank you for this, finally someone answered. I did say the almost similar thing during the interview.

This is surprisingly common and leaves out a lot of devs with strong potential that just haven't been exposed to the concepts in question.

This sums up my life in past 1 year. Thank you!

You seem really humble and mature with your approach while interviewing. This is like broadening my horizons Thank you and I guess no wonder I haven't heard from them. Maybe on Monday. Till then hustling more with my own Project.

Thread Thread
 
admwx7 profile image
Austin Murdock • Edited

It's also possible they haven't done many interviews themselves and were just as nervous as you. I still sometimes get nervous before interviewing people, if I screw up I could potentially cost someone their chance at a job.

I personally don't anticipating hearing back for at least 2 full business days and try not to get concerned unless a full business week has passed. I have a colleague that actually got an offer after 6 weeks of radio silence, I've personally had offers before I left the premise to the longest taking nearly 3 weeks from the final interview. I never anticipate hearing back from a Thursday / Friday interview before the following week, I know far too many people that like to take Fridays off early or completely.

Thread Thread
 
oathkeeper profile image
Divyesh Parmar

Wow thank you for soothing words. It really means a lot.

 
oathkeeper profile image
Divyesh Parmar • Edited

No I did repeated that I understood and stuff and that's why I started trying even though I had no prior knowledge of polling function

It would also need to keep track of the returned value for each promise that fulfils to be correct,

Cool exactly what I was waiting for since it needs to show all the resolved ones.

Collapse
 
oathkeeper profile image
Divyesh Parmar

I think the reason he dropped your polling function is because you didn't make it return anything.

Yeah because I mentioned him 3 times that I don't know about this function and I was still talking about my approach on how it should go ahead. Same for the PromiseAll question.

Promise.all accepts an array of promises and returns a promise.

I do understand this but what confuses me is that I have read it on javascript.info and other resources as well that it returns an array of resolved promises, is that correct? javascript.info/promise-api

Thank you for the positive response I feel good about it now! Hahaha! BTW how much is it good to be judged only on basis of this? I feel that it shouldn't go this way all the time, right? This isn't my first time and as you can see from my previous posts I have given many of such!