Typescript Basics Flashcards

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) { … }

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)

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)

17
Q

Convert string to number

A

Number(x)

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>

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) {…}

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
Q

Set the largest (smallest) number possible to the variable max (min)

A

const max = Infinity;
const min = -Infinity;

26
Q

Determine if all elements in an array are below 4

A

[1, 2, 3].every((value) => value < 4)

27
Q

Given an array, remove all elements that are less than 0

A

[-1, 2, 3].filter((value) => value >= 0);

28
Q

Find the first index in an array for a specific value

A

[1, 2, 3].findIndex((el) => el === 2);
OR arr.indexOf(2)

29
Q

Find the last index in an array of a specific value

A

[1, 2, 2, 3].findLastIndex((el) => el === 2);
OR arr.lastIndexOf(2)

30
Q

Given the array [1, 2, [3, [4]]], generate the array [1, 2, 3, 4]

A

[1, 2, [3, [4]]].flat(Infinity)

31
Q

*Determine if an array contains a value

A

[1, 2, 3, 4].includes(3);

32
Q

*Determine if a variable is an array type

A

Array.isArray(myVar);

33
Q

How do you run a function every n milliseconds?

A

const timerId = setInterval(() => myFunction, n);
clearInterval(timerId);

34
Q

Create a new date

A

const date = new Date(year, month, day)
// note, month is zero-indexed

35
Q

How do you check if a variable is an object?

A

typeof x === ‘object’ && x !== null && !Array.isArray(x)