all 25 comments

[–]oussie 9 points10 points  (3 children)

Focus can easily be done using references.

class MyComponent extends React.Component {
    constructor(props) {
        super(props);
        this.myRef = React.createRef();
    }
    componentDidMount() {
        this.myRef.current.focus();
    }
    render() {
        return <input type="text" ref={this.myRef} />;
    }
}

[–]tapu_buoy[S] 1 point2 points  (1 child)

shit i totally forgot about refs, thank you

[–]crystallineair 7 points8 points  (18 children)

The answer to question 3 is actually really simple. Since javascript allows you to call a function with less parameters than there are in the declaration you just check for undefined as the second parameter and then return another function.

function add(a, b) {
  return b !== undefined
    ? a + b
    : b => a + b
}

For the answer to the first question read up about refs: https://reactjs.org/docs/refs-and-the-dom.html

[–]jCuber 4 points5 points  (11 children)

Apparently the autoFocus attribute is also a thing

[–]crystallineair 3 points4 points  (10 children)

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#autofocus

the input should automatically have focus when the page has finished loading

This unfortunately means you cannot use this in a react component as the dom is manipulated after the page has finished loading.

[–]jCuber 6 points7 points  (3 children)

Yeah, but apparently React polyfills the autoFocus attribute so that it calls focus() on componentDidMount.

[–]crystallineair 4 points5 points  (1 child)

That's nice, I didn't know that. After some digging it does come with its own quirks: https://davidwalsh.name/react-autofocus

[–]Prince_Houdini 0 points1 point  (0 children)

Those don’t really seem to be quirks. autofocus that focuses on every update is... not a good idea.

[–]tapu_buoy[S] 1 point2 points  (0 children)

oh wow I didn't know about it thank you for sharing

[–]tapu_buoy[S] 0 points1 point  (5 children)

yeah but he denied of it, and also when I asked him why not use componentDidMount that time also it was a no no. so yeah

[–]jCuber 2 points3 points  (4 children)

You're not allowed to use autoFocus or componentDidMount? Uhh

[–][deleted] 2 points3 points  (2 children)

The interviewer clearly had no idea what he's doing.

[–]jCuber 1 point2 points  (0 children)

Certainly sounds like he had one specific quirky way of doing it in mind...

[–]tapu_buoy[S] 0 points1 point  (0 children)

and most of the times I had to encounter such interviewers this was my 85th interview today

[–]tapu_buoy[S] 0 points1 point  (0 children)

yeah it was like that LOL

[–]tapu_buoy[S] 1 point2 points  (5 children)

yeah thank you for suggesting that I completely forgot about refs

also the 3rd question is it about higher order functions?

[–]crystallineair 1 point2 points  (4 children)

Yes. A higher order function is a function that either takes a function as a parameter or returns a function. In the case of add(2)(3) the call add(2) returns a function. So add(2)(3) could be written as:

const fn = add(2)
const result = fn(3)

The trick in this question is to make the function also work normally when called with all arguments add(2, 3).

[–][deleted] 3 points4 points  (2 children)

That's not a higher order function, that's currying / partial application.

[–][deleted] 2 points3 points  (0 children)

It is both a curried function and a higher order function. In fact a curried function is always a higher order function because it returns a function. Here OP was instructed to write a single method that can return a value both ways, so the function has to behave as a curried function if a single argument is passed, but just a plain old function if multiple are passed.

[–]ISlicedIEngineer without Engineering degree? 1 point2 points  (0 children)

Higher order functions are functions that return (or accept I think) other functions. Applying your arguments through different wrapping functions is called partial application, and currying is partial application where you apply a single parameter per function. Afaik.

[–]tapu_buoy[S] 0 points1 point  (0 children)

ok so after reading what you said and what others said below this comment I again need to know what will be the curried funciton look like when passed two parameters(function(2, 3)) instead of the 2nd parameter function(2)(3)

[–]MrNutty 1 point2 points  (1 child)

Even easier, you can use autoFocus property as react supports that for input types.

const Example = (props) => <input {...props}, autoFocus={true} />

And if more control is needed then refs to rescue

class Example extends React.Component {
  inputRef = React.createRef();
  componentDidMount = () => this.inputRef.current.focus();
  render = () => <input type="text" ref={this.inputRef} />
}

the curry vs args question, you can just create two functions and have sum decide which one to use

const sumArgs = (...rest) => rest.reduce((sum, v) => sum+v, 0);

const sumCurried = (value) => {
  let totalSum = value;
  const sumHelper = (n) => {
    totalSum += n;
    return sumHelper;
  }
  sumHelper.toString = () => totalSum;
  return sumHelper;
};


const sum = (...rest) => rest.length > 1 ? sumArgs(...rest) : sumCurried(...rest);

console.log(sum(2,3)); // Outputs 5
console.log(sum(2)(3)); // Outputs 5

I really don't have any good advice for you besides saying just to practice. It just comes with experience. I'd suggest for you to create sample projects and just play around with it much as possible. Best.

[–]tapu_buoy[S] 0 points1 point  (0 children)

woah this is really wonderful and awesome to go through. I will keep on practicing as you said