3

some tricky javascript tests

I have collected some tricky tests of javascript that I encountered in job interviews, which are pretty interesting. They covered some essential aspects in Javascript that we might don’t pay attention in practice but very important.

  1. what are the results of console.log(a) and console.log(b) ?
  2. use the most efficient way to build the caculator function.
  3. what will console.log show as result
  4. what’s the result of each console.log
  5. what’s the result of console.log
  6. tell us the result of each line
  7. why 0.1 +0.2 == 0.3 return false, but 0.1+0.3 == 0.4 return true ?

Answers are in next page…

Darktalker

3 Comments

  1. For #2, obeying the priority of operators, shouldn’t the value of -5+2*2 be -5+(2*2), i.e. -1?

  2. To make it work properly you need to create the whole equation as a string and eval it all at once, then js will sort out the order of operators itself. So you want something like

    var equation=’0′;
    for (var index in t) {
    equation+=t[index][0]+t[index][1];
    }
    return eval(equation);

  3. in fact, the caculation should be step by step like a real calculator, the question asks you to do like this:

    //step 1
    var result = -5+2;
    //step 2
    result = result * 2;

    at first, I didn’t understand the question neither 🙂

Leave a Reply