37

Real story at a screening interview:

Me(interviewer): how do you convert a string into a number in Javascript?
Candidate: what do you mean?
Me: (writes "123" -> 123 on paper) How do you convert "123" to 123 in JS?
Candidate: (starts writing program on paper)

str.replace(/\"/g, '"")

mind==blown!!

Comments
  • 12
    Honest question. How does it typically go over if I answer "I don't know but it would be quick to look up" to those type of questions?

    It makes me curious since that particular question is something I've done a million times, but I actually can't remember from the top of my head whether it was a function from the string or if you passed the string as the argument.
  • 2
    @ltlian isn't it just parseInt?
  • 5
    @gitpush yeah, but I had to check mdn real fast if it was "123".parseint() or parseint(" 123"). Takes 15 seconds but it still means I can't say for sure when asked
  • 1
    @ltlian I only asked cuz I'm a newbie in web dev and thought there is another way lol
  • 7
    It depends on the company hiring and the position.

    If its a junior position and a good recruiter I say they would try other questions and if you can solve those this should not be a problem.

    Especially if you have specified other languages than js but not specifically js.

    We usually do not ask language specific questions like that, instead we present a problem and ask them to describe either in code, pseudo code or english how they would go about solving it.

    This shows if they have the mindset to solve problems.

    Just knowing the language is like assuming that someone that could translate 10 000 words also could write a good book.
  • 2
    I once did +str to convert to int. Idk if that's legal. Saw Wes Bos doing it
  • 0
    @samsepiol I think this unary operator is a typescript way of converting.
  • 3
    I can see how this could be confusing..
    Interviewer: how to convert sting to number?
    Me: Number?
    Interviewer: Yes how to convert to a number?
    Me: Number function?
    Interviewer: yes what is the function to convert to a number
    Me: it is number
    Interviewer: no it is a string and we need to convert it
    Me: Use number to convert it. Number. NUMBER then open bracket, then string, then close bracket.
  • 2
    @ltlian I trust a dev who verifies MDN when they’re not 100% sure over one who bluffs it and tries to act smart.
  • 1
    I mean I would have just said "123" - 1 + 1
  • 1
    n = 0
    for c in "123":
    n *= 10
    n += ord(c) - ord("0")
  • 0
    +"123"
Add Comment