r/coding • • Aug 09 '17

Test your Javascript skills with our fun Javascript test!

https://www.atlascode.com/blog/test-your-javascript-skills-with-our-fun-javascript-test/
24 Upvotes

9 comments sorted by

8

u/bigjoebegs Aug 09 '17

Three quick things:

  1. The correct answer to #4 is actually "B".

  2. ParseInt isn't really relevant. I never use it and I don't know why knowing parseInt('ABC') would be useful.

  3. I put quotes around my string answers and got one wrong because of it (wrote 'AB' when the answer was AB)

All around, nice work, but this needs touching up.

3

u/MellerTime Aug 09 '17

I really hated the "other" option. It's not like I can't go through and tell that I actually got the answer right, but for any online quiz that just seemed a dumb way to handle it.

1

u/TheAtlasTeam Aug 10 '17

The answer to question 4 is in fact undefined. This question is designed to show the results of hoisting. Unfortunately chrome's developer console has undergone some changes in the last few months which mean that it no longer hoists variable declarations like it should. You can see the results of hoisting in question 4 if you run the script in the page rather than the console like in this fiddle. Here you will see undefined correctly being reported in the console.

Give yourself a point for 'AB' :)

1

u/DEiE Aug 10 '17

It depends on how you run the code. If you run it within a function, which Fiddle is doing by default, it will hoist the var variable1 declaration to the top of the function so all values will be assigned to variable1 within the function. window.variable1 will be undefined.

If you don't wrap it in a function and execute it directly in a <script> tag, it will actually make variable1 global (i.e. put it on the window), so it will print "b".

See this fiddle: https://jsfiddle.net/n4x1fu1z/. You can change the way the code is run by clicking the little cog next to the JavaScript label.

0

u/ItsAllInYourHead Aug 09 '17
  1. You're wrong: variable1 is hoisted at line 3.

  2. What do you mean by "isn't really relevant"? Isn't relevant to what?

3

u/bigjoebegs Aug 09 '17

Admittedly I was wrong about parseInt(). I've just been using "+" in front of my string, but after digging into it you guys are right.

Not sure what you're talking about in your first point.

-3

u/ItsAllInYourHead Aug 09 '17

Well perhaps you should read up on variable hoisting then.

0

u/[deleted] Aug 09 '17

[deleted]

1

u/SanityInAnarchy Aug 10 '17

Relying on it ignoring trailing characters sounds like a recipe for disaster. Not everyone uses px all the time. jQuery has some clever tricks for calculating actual height in pixels, regardless of the CSS property.

I might use it on user input in form fields, though it's tempting to use hacks like '*1' instead.

1

u/SanityInAnarchy Aug 10 '17

Question 6 will mark you wrong for differences in spacing. This is extra bullshit because it relies on the behavior of console.log(), which is a "living standard" that ultimately gives multiple possible right answers, some implementation-defined, not even all can be represented as strings -- Chrome represents generic objects as an interactive tree structure, for example.

Even if you "cheat", you get it wrong -- if you actually paste this snippet into Chrome, it spits out this exact string:

function (){ 
        return 'A';
    }

But the test is expecting this one:

function(){return 'A';
}

To make this even worse, when it "grades" you, it puts both your answer and the "correct" answer in <p> tags, verbatim -- no <pre>, no hacks with <br> or &nbsp;, it just tells you that you entered function(){ return 'A'; }, and it wanted function(){return 'A'; }.

I can't be too hard on them, though, because there isn't a good way to fix this with the way they want to use console.log(x) as a shorthand for "What is the value of x?" ...so even if they used a JS parser and compared some fully-normalized ASTs, console.log() would still make them not-quite-correct.