Typescript Basics Flashcards

(40 cards)

1
Q

Loop through an object’s keys and values

A

for (const [key, value] of Object.entries(animals)) {
console.log(${key}: ${value});
}

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

Check if a key is in an object

A

if (key in myObject) { … }

OR

myObj.hasOwnProperty(key)

OR

Object.hasOwn(obj, key)

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

*Loop through values in an array

A

for (const num of nums) { … }

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

Sort an array in ascending order

A

array.sort((a, b) => a - b)

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

Define a tree structure (eg BST)

A

class TreeNode {
value: number;
left: TreeNode | null;
right: TreeNode | null;

constructor(value: number) {
this.value = value;
this.left = null;
this.right = null;
}
}

const node = new TreeNode(10)

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

Write a switch statement

A

const expr = ‘Papayas’;
switch (expr) {
case ‘Oranges’:
console.log(‘Oranges are $0.59 a pound.’);
break;
case ‘Mangoes’:
case ‘Papayas’:
console.log(‘Mangoes and papayas are $2.79 a pound.’);
break;
default:
console.log(Sorry, we are out of ${expr}.);
}

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

Round decimal value down to nearest integer

A

Math.floor(n)

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

Round decimal value up to nearest integer

A

Math.ceil(n)

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

Round decimal value towards zero

A

Math.trunc(n)

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

Remove first element from array

A

const value = array.shift()

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

Get the max value in an array

A

Math.max(…array)

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

Create an empty array of n items

A

new Array(n).fill(null)
new Array(n).fill()

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

*Loop through indexes and values of array

A

for (const [index, value] of myArray.entries()) { … }

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

Remove key from object

A

delete myObj[key]

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

Get array of keys from object

A

Object.keys(myObj)

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

Convert number to string

A

n.toString()
OR
String(n)

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

Convert string to number

18
Q

Create a class called Range that takes from and to as number arguments and includes a method called toString that prints both. Then instantiate an instance of the class and call toString

A

class Range {
constructor(from, to) {
this.from = from;
this.to = to;
}
toString() {
return ${this.from}...${this.to};
}
}

const range = new Range(1, 4);
range.toString();

19
Q

Create an async sleep function that resolves after n milisecondes

A

const sleep = async (n: number): Promise<void> => {
return new Promise(resolve => setTimeout(resolve, n));
}</void>

You would call this with await sleep(n)

20
Q

Create a timeout and cancel a timeout

A

const timeout = setTimeout(() => {…}, 10);
clearTimeout(timeout);

21
Q

Given an array, return the 1st to 3rd (inclusive) elements as an array

A

array.slice(0, 3)

22
Q

*Implement and use a Map (and iterate through keys and values of map)

A

const myMap = new Map();
myMap.set(‘a’, 1)
myMap.get(‘a’)
myMap.has(‘a’)
myMap.size // 1
myMap.delete(‘a’)

for (const [key, value] of myMap) {…}

To convert a map to an object, use Object.fromEntries(myMap)

Can also create a map like this:
new Map([['a', 1], ['b', 2]]);

23
Q

*Implement and use a Set (and iterate through values of set)

A

const mySet = new Set(); // use new Set(array) to convert array to set
mySet.add(‘a’);
mySet.has(‘a’); // true
mySet.size; // 1
mySet.delete(‘a’);

for (const item of mySet) {…}
const myArray = […mySet]

24
Q

Use reduce method to sum numbers in an array

A

[1, 2, 3].reduce((acc, curr) => acc + curr, 0)

25
Set the largest (smallest) number possible to the variable max (min)
const max = Infinity; const min = -Infinity;
26
Determine if all elements in an array are below 4
[1, 2, 3].every((value) => value < 4)
27
Given an array, remove all elements that are less than 0
[-1, 2, 3].filter((value) => value >= 0);
28
Find the first index in an array for a specific value
[1, 2, 3].findIndex((el) => el === 2); OR arr.indexOf(2)
29
Find the last index in an array of a specific value
[1, 2, 2, 3].findLastIndex((el) => el === 2); OR arr.lastIndexOf(2)
30
Given the array [1, 2, [3, [4]]], generate the array [1, 2, 3, 4]
[1, 2, [3, [4]]].flat(Infinity)
31
*Determine if an array contains a value
[1, 2, 3, 4].includes(3);
32
*Determine if a variable is an array type
Array.isArray(myVar);
33
How do you run a function every n milliseconds?
const timerId = setInterval(() => myFunction, n); clearInterval(timerId);
34
Create a new date
const date = new Date(year, month, day) // note, month is zero-indexed
35
How do you check if a variable is an object type?
typeof x === 'object' && x !== null && !Array.isArray(x)
36
Add an element to the end of an array
array.push(value);
37
How do you deep clone an object?
structuredClone(obj) [newer] OR JSON.parse(JSON.stringify(obj))
38
What is even bubbling and how do you stop it?
Event bubbling is when an event propagates from the target up through parent elements. You stop it with event.stopPropagation()
39
What is closure?
A closure is what happens when a function "remembers" variables from the scope it was created in, even after that scope is gone; closures are how JS maintains state across function calls
40