4 - Data Structures: Objects and Arrays Flashcards

1
Q

How to access a property of an object?

A

Using the bracket, or a dot notation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do you retrieve by accessing a property?

A

Value that property references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which objects don’t have properties?

A

‘null’ and ‘undefined’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the return value of typeof null?

A

‘object’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a requirement for a property name so that you can access it with a dot notation?

A

It must follow the rules of the bindings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What property names do you use to retrieve values from an array?

A

Numbers. They start with ‘0’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a method?

A

Property on an object that references a function value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Conceptually, what kind of a data structure is an array?

not in the JS context

A

Stack. LIFO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a JS object in regard to what it contains?

A

Arbitrary collections of properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to syntactically write a property name of an object which doesn’t adhere to the rules of naming a binding?

A

Using quotes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What purposes have braces in general?

A

To use for a set of statements and a notation for an object literal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will be returned when you access a property that doesn’t exist on the object?

A

‘undefined’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to remove a property from an object?

A

delete obj.foo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to check if an object has a property?

A

‘a’ in object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to find all properties of an object?

A

Object.keys(obj)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to copy all properties from one object to another?

A

Object.assign(toObj, fromObj)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the return value of ‘typeof []’

A

object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which data types are not mutable?

A

Strings, numbers, booleans, null and undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to compare if two bindings have reference to the same object?

A

let o1 = { foo: 1 }
let o2 = o1;
o1 == o2 // true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Is this valid const o = { a: 1 }; o.b = 2? Why?

A

Yes.

It mutates the elements of an object and not the assignment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How is the comparison with == of two different objects called?

A

Identity comparison.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to write classical for loop in a easier way?

A

for (let elem of collection) {

for…of

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What other objects can you loop over with for..of loop?

A

Strings, objects, but iterables in general.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What do ‘unshift’ and ‘push’ do?

A

unshift -> add element from the left
push -> add element from the right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What do ‘shift’ and ‘pop’ do?

A

shift -> remove element on the left
pop -> remove element on the right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What does indexOf do?

A

Finds the index of the first matching element. [1,2,3].indexOf(2) => 3

27
Q

What do indexOf and lastIndexOf return when they can’t find an element?

A

-1

28
Q

What does lastIndexOf do?

A

Finds the index of the first matching element searching from the right. [1,2,3].indexOf(2) => 1

29
Q

How Array.slice behaves?

A

It creates a new array from the one it was called on.

[1, 2, 3, 4, 5].slice(2) => [3, 4, 5]
[1, 2, 3, 4, 5].slice(2, 4) => [3, 4]

30
Q

How many elements Array.slice takes? Which element is inclusive, and which is exclusive?

A

Two elements. First one is inclusive, the second one is exclusive.

31
Q

Will this work ‘[1, 2, 3, 4, 5].concat(3)’?

A

Yes.

It can concatenate non-array values.

32
Q

How to join two arrays using a Array function?

A

Array.concat.

33
Q

Why can’t you add a new property to a string?

A

Because strings are not objects.

34
Q

Why do strings have properties even though they are immutable?

A

Because they inherit properties from String.prototype.

35
Q

Do strings have .slice and .indexOf properties?

A

Yes.

36
Q

What does String.trim do?

A

Removes whitespace from the start and end of the string.

37
Q

How does String.padStart work?

A

‘6’.padStart(4, ‘1’) // => ‘1116’

38
Q

How does String.split work?

A

It splits the string on a given char.

39
Q

How does String.join work?

A

It doesn’t exist.

40
Q

How to repeat a string ‘foo’ three times?

A

With String.repeat => ‘foo’.repeat(3)

41
Q

How can you access letter ‘x’ in a string ‘000x00’?

A

‘000x00’[3]

42
Q

How can you “collect” multiple arguments to a function to a single parameter?

A

With rest param. function foo(…args)

43
Q

If you have an array and want to pass its elements to a function as arguments, how do you do it?

A

foo(…ary)

44
Q

Why is spread operator called so?

A

Because it “spreads” elements of an array in place as separate values.

45
Q

What is the Math object by itself technically? Why is it needed?

A

It acts as a namespace for Math.sum, Math.sqrt etc so that the global namespace isn’t polluted by those functions.

46
Q

What does it mean to “pollute” a global namespace?

A

Global namespace is polluted when bindings are defined in the outer most scope. It increases the likelihood of overriding some binding.

47
Q

How does Javascript warn you when you redeclare bindings created with var and function keyword?

A

It doesn’t.

48
Q

What keywords do you have to use so that Javascript warns you when you redeclare a binding?

A

let and const

49
Q

What is the return value of Math.random?

A

A pseudorandom number between (inclusive) zero and (exclusive) one.

50
Q

How are random numbers conceptually created considering that computers are deterministic machines?

A

By creating a hidden value which gets modified every time a new random number gets created.

51
Q

What is the return value of Math.floor(8.23)?

A

8

52
Q

What is the opposite of Math.floor?

A

Math.ceil

53
Q

Which function will round the given fractional number up or down?

A

Math.round

54
Q

What is the return value of Math.round(0.50)?

A

1

55
Q

How do you ensure you always get a positive number from any given number input?

A

Math.abs

56
Q

If the function accepts an array with two elements as an argument, how do you assign those elements in the function signature?

A

function foo([firstElemt, secondElem])

57
Q

What is the value of binding z here ‘let [z] = null’

A

Type error: null is not iterable

58
Q

How many memory addresses are created with ‘let o = { a: [1, 2] }’

A

Two. One for the object “o” and the other for the object referenced by the “a” property.

59
Q

How do you send Javascript literal object over the network?

A

You can’t. You have to serialize the object.

60
Q

How do you serialize the Javascript object?

A

JSON.stringify

61
Q

How do you deserialize JSON object?

A

There are no JSON objects. JSON is a string which is deserialized with JSON.parse.

62
Q

What is the syntax for putting comments within JSON files?

A

JSON doesn’t support comments.

63
Q

Which quotes are available in JSON format?

A

Double quotes.

64
Q
A