JavaScript Test

For each item except 37 and 38, answer what the JavaScript expression evaluates to. If the expression throws an exception or error upon evaluation, indicate which exception or error is thrown.

Part A. One Divided by Zero

> 1/0
1. ______
> 1/-0
2. ______
> -1/-0
3. ______

Part B. Two Arrays Walk into a Foo

> []+[]
4. ______
> []-[]
5. ______
> []*[]
6. ______
> []/[]
7. ______
> []%[]
8. ______
> []<[]
9. ______
> []==[]
10. ______
> []<=[]
11. ______
> []<<[]
12. ______
> []>>[]
13. ______

Part C. Fifty Shades of Zero

> 0==””
14. ______
> 0==”0″
15. ______
> “”==”0″
16. ______
> 0+”0”
17. ______
> 0-“0”
18. ______
> 0==[]
19. ______
> []==0
20. ______
> 0==[0]
21. ______
> “0”==[0]
22. ______
> “0”==[“0”]
23. ______
> [0]==[“0”]
24. ______
> 0==[“0″]
25. ______
> [0]+[0]
26. ______
> [0]-[0]
27. ______
> [0]*”0”
28. ______
> []+{}
29. ______
> {}+[]
30. ______
> {}-[]
31. ______
> {}=={}
32. ______
> 0=={}
33. ______
> {}==0
34. ______
> 0=={0:0}
35. ______
> [0]+{0:0}
36. ______

37. What type is the result of []+{}+0, that is, adding the empty array, the empty object, and 0?
a. array
b. object
c. integer
d. number
e. this expression SyntaxErrors
f. none of the above

38. If you didn’t select e above, what exactly does []+{}+0 evaluate to? If you selected e above, what is the token that the SyntaxError complains about?

Part D. Array Operations

> var foo = [20, 5, 15, 10]
> var bar = foo.reverse()
> foo
39. ______
> bar
40. ______
> var foo = [20, 5, 15, 10]
> var bar = foo.sort()
> foo
41. ______
> bar
42. ______
> var foo = [20, 5, 15, 10]
> var bar = foo.concat([5])
> foo
43. ______
> bar
44. ______
> var foo = [20, 5, 15, 10]
> var bar = foo.concat(5)
> foo
45. ______
> bar
46. ______

Part E. Parsing to Integer

> parseInt(“10”)
47. ______
> parseInt(“1/0”)
48. ______
> parseInt(“10”,8)
49. ______
> parseInt(1/0,8)
50. ______
> parseInt(1/0,12)
51. ______
> parseInt(1/0,16)
52. ______
> parseInt(1/0,20)
53. ______
> parseInt(1/0,24)
54. ______
> parseInt(1/0,28)
55. ______
> parseInt(1/0,32)
56. ______
> parseInt(1/0,36)
57. ______
> parseInt(1/0,40)
58. ______

To check your answers (since there’s some pretty involved arithmetic here): out of your answers to items 50 through 58, the sum of your answers that are integers should be 43 mod 127 and 41 mod 151.

Part F. Putting it All Together

javascript_function

In case this “reassuring” disclaimer may be necessary: There are no typos in the writing of this function. Every single character (or lack thereof) is intentional for the purposes of the following items.

> quux(2,4,5)
59. ______
> quux(100,[“foo”],”foo”)
60. ______
> quux(0,0,0)
61. ______
> quux(0,0)
62. ______
> quux(0)
63. ______
> quux()
64. ______
> quux(0,0,0,0)
65. ______
> a
66. ______
> b
67. ______
> c
68. ______
> d
69. ______
> e
70. ______
> f
71. ______
> 0+quux
72. ______

As a final check of your work, exactly 7 of these 72 expressions throw an exception or error upon evaluation. You have exactly 7, right?

Answers

Part A. One Divided by Zero

> 1/0
1. Infinity
> 1/-0
2. -Infinity
> -1/-0
3. Infinity

Part B. Two Arrays Walk into a Foo

> []+[]
4. “” (the empty string)
> []-[]
5. 0
> []*[]
6. 0
> []/[]
7. NaN
> []%[]
8. NaN
> []<[]
9. false
> []==[]
10. false
> []<=[]
11. true
> []<<[]
12. 0
> []>>[]
13. 0

Part C. Fifty Shades of Zero

> 0==””
14. true
> 0==”0″
15. true
> “”==”0”
16. false
> 0+”0″
17. “00”
> 0-“0”
18. 0
> 0==[]
19. true
> []==0
20. true
> 0==[0]
21. true
> “0”==[0]
22. true
> “0”==[“0”]
23. true
> [0]==[“0”]
24. false
> 0==[“0”]
25. true
> [0]+[0]
26. “00”
> [0]-[0]
27. 0
> [0]*”0″
28. 0
> []+{}
29. “[object Object]”
> {}+[]
30. 0
> {}-[]
31. -0
> {}=={}
32. SyntaxError
> 0=={}
33. false
> {}==0
34. SyntaxError
> 0=={0:0}
35. false
> [0]+{0:0}
36. “0[object Object]”

37. What type is the result of []+{}+0, that is, adding the empty array, the empty object, and 0?
a. array
b. object
c. integer
d. number
e. this expression SyntaxErrors
f. none of the above

38. If you didn’t select e above, what exactly does []+{}+0 evaluate to? If you selected e above, what is the token that the SyntaxError complains about?

“[object Object]0”

Part D. Array Operations

> var foo = [20, 5, 15, 10]
> var bar = foo.reverse()
> foo
39. [10, 15, 5, 20]
> bar
40. [10, 15, 5, 20]
> var foo = [20, 5, 15, 10]
> var bar = foo.sort()
> foo
41. [10, 15, 20, 5]
> bar
42. [10, 15, 20, 5]
> var foo = [20, 5, 15, 10]
> var bar = foo.concat([5])
> foo
43. [20, 5, 15, 10]
> bar
44. [20, 5, 15, 10, 5]
> var foo = [20, 5, 15, 10]
> var bar = foo.concat(5)
> foo
45. [20, 5, 15, 10]
> bar
46. [20, 5, 15, 10, 5]

Part E. Parsing to Integer

> parseInt(“10”)
47. 10
> parseInt(“1/0”)
48. 1
> parseInt(“10”,8)
49. 8
> parseInt(1/0,8)
50. NaN
> parseInt(1/0,12)
51. NaN
> parseInt(1/0,16)
52. NaN
> parseInt(1/0,20)
53. 18
> parseInt(1/0,24)
54. 151176378
> parseInt(1/0,28)
55. 324267766
> parseInt(1/0,32)
56. 20115447389
> parseInt(1/0,36)
57. 1461559270678
> parseInt(1/0,40)
58. NaN

Part F. Putting it All Together

function quux(a,b,c) {
    var d = 1;
    e = 2;
    if(b==c) {
        d++
        var f = a==b;
     }
     if(f) {
         e += a;
     }
     e +=a;
     return e*d;
}

> quux(2,4,5)
59. 4
> quux(100,[“foo”],”foo”)
60. 204
> quux(0,0,0)
61. 4
> quux(0,0)
62. 2
> quux(0)
63. 4
> quux()
64. NaN
> quux(0,0,0,0)
65. 4
> a
66. ReferenceError
> b
67. ReferenceError
> c
68. ReferenceError
> d
69. ReferenceError
> e
70. 2
> f
71. ReferenceError
> 0+quux
72. “0function quux(a,b,c) {\n    var d = 1;\n    e = 2;\n    if(b==c) {\n        d++\n        var f = a==b;\n    }\n    if(f) {\n        e += a;\n        }\n    e +=a;\n    return e*d;\n}”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s