JavaScript Flashcards

1
Q

get ASCII code of a letter

A

“abcd”.charCodeAt(0)

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

get letter from ASCII code

A

String.fromCharCode(97)

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

string to array

A

str.split(“”) or str.split(“,”) or str.split(/[|,]/g)

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

match a string in a sentence

A

let text = “The rain in SPAIN stays mainly in the plain”;
let matchArray = text.match(/ain/gi);

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

Check if a string includes “world”

A

text.includes(“world”);

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

to return a decimal after a specified precision (rounded off)

A

let x = 9.656;
x.toFixed(0);
x.toFixed(2);
x.toFixed(4);

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

Check if the value is an integer

A

Number.isInteger(x)

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

sorting an integer array

A

array.sort() // asending
array.sort().reverse() // descending
array.sort( (a, b) => b - a )

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

sorting a string array

A

array.sort() // asending
array.sort().reverse() // descending
array.sort( (a,b) => b.localeCompare(a) ) ) // descending

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

convert array into string ?

A

array.join(“”)

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

concatenating 2 arrays

A

const mergedArray = array1.concat(array2);

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

splice() to Remove Elements from Array

A

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.splice(0, 1); // [“Orange”, “Apple”, “Mango”];

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

take a slice of array

A

const fruits = [“Banana”, “Orange”, “Lemon”, “Apple”, “Mango”]; const citrus = fruits.slice(1, 3); // [“Orange”, “Lemon”]

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

Create an array from a string

A

let array = Array.from(“ABCDEFG”)

it has same effect as let array = “ABCDEFG”.split(“”)

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

Print all values of an array

A

for(let x of array) {
console.log(x)
}

// prints the values. “of” is for values

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

Print the indexes of an array

A

for(let x in array) {
console.log(x)
}

// prints indexes. “in” is for indexes

17
Q

Remove duplicates from an Array

A

let array = [1,2,3,3,4,1,2,5,6,7,8,8]
let set = new Set(array);

18
Q

Iterating a Set

A
for( let x of set.values() ) {
   console.log(x)
}
	
	
19
Q

Iterating a Map

A
for( let x of map.keys() ) {
  console.log(fruits.get(x))
}
20
Q

Define a class in JS

A

class Car {
constructor(name, year) {
this.name = name;
this.year = year; }
age() {
const date = new Date(); return date.getFullYear() - this.year;
}
}

21
Q

RegEx for a whitespace character

A

/\b/

22
Q

RegEx for alphanumeric character

A

/\w/
This includes [A-Za-z0-9_]
if we want to extend this to special characters, we need to add
[\w@#$]

22
Q

RegEx for alphanumeric character

A

/\w/
This includes [A-Za-z0-9_]
if we want to extend this to special characters, we need to add
[\w@#$]