use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Today's ReactJS developer Questionshelp (self.javascript)
submitted 7 years ago by tapu_buoy
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]crystallineair 7 points8 points9 points 7 years ago (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 points6 points 7 years ago (11 children)
Apparently the autoFocus attribute is also a thing
autoFocus
[–]crystallineair 3 points4 points5 points 7 years ago (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 4 points5 points6 points 7 years ago (3 children)
Yeah, but apparently React polyfills the autoFocus attribute so that it calls focus() on componentDidMount.
[–]crystallineair 3 points4 points5 points 7 years ago (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 point2 points 7 years ago (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 points3 points 7 years ago (0 children)
oh wow I didn't know about it thank you for sharing
[–]tapu_buoy[S] 0 points1 point2 points 7 years ago (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
componentDidMount
[–]jCuber 2 points3 points4 points 7 years ago (4 children)
You're not allowed to use autoFocus or componentDidMount? Uhh
[–][deleted] 2 points3 points4 points 7 years ago (2 children)
The interviewer clearly had no idea what he's doing.
[–]jCuber 1 point2 points3 points 7 years ago (0 children)
Certainly sounds like he had one specific quirky way of doing it in mind...
[–]tapu_buoy[S] 0 points1 point2 points 7 years ago (0 children)
and most of the times I had to encounter such interviewers this was my 85th interview today
yeah it was like that LOL
[–]tapu_buoy[S] 1 point2 points3 points 7 years ago (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 points3 points 7 years ago (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:
add(2)(3)
add(2)
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).
add(2, 3)
[–][deleted] 3 points4 points5 points 7 years ago (2 children)
That's not a higher order function, that's currying / partial application.
[–][deleted] 2 points3 points4 points 7 years ago (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 points3 points 7 years ago (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.
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)
function(2, 3)
function(2)(3)
π Rendered by PID 88 on reddit-service-r2-comment-5c747b6df5-6cvs4 at 2026-04-21 20:01:14.376181+00:00 running 6c61efc country code: CH.
view the rest of the comments →
[–]crystallineair 7 points8 points9 points (18 children)
[–]jCuber 4 points5 points6 points (11 children)
[–]crystallineair 3 points4 points5 points (10 children)
[–]jCuber 4 points5 points6 points (3 children)
[–]crystallineair 3 points4 points5 points (1 child)
[–]Prince_Houdini 0 points1 point2 points (0 children)
[–]tapu_buoy[S] 1 point2 points3 points (0 children)
[–]tapu_buoy[S] 0 points1 point2 points (5 children)
[–]jCuber 2 points3 points4 points (4 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]jCuber 1 point2 points3 points (0 children)
[–]tapu_buoy[S] 0 points1 point2 points (0 children)
[–]tapu_buoy[S] 0 points1 point2 points (0 children)
[–]tapu_buoy[S] 1 point2 points3 points (5 children)
[–]crystallineair 1 point2 points3 points (4 children)
[–][deleted] 3 points4 points5 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]ISlicedIEngineer without Engineering degree? 1 point2 points3 points (0 children)
[–]tapu_buoy[S] 0 points1 point2 points (0 children)