Javascript Built In Methods Flashcards

1
Q

String()

A

Turns primitive strings into objects

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

What is the difference between forEach, map, filter and reduce?

A

forEach does not transform or return any values and uses callbacks

map transforms the values in an array but does not ‘mutate’ or change the number of elements in an array

filter has a callback that returns a boolean which decides whether or not to include the original value in the result

reduce combines the values in an array to return 1 result

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

forEach

A

takes a function and applies that function into every item in an array

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

map

A

creates and returns a new array after it applies a function over every item in a collection

[1, 2, 3].map(x => x + 1) // [2, 3, 4]

map DOES NOT change the collection

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

filter

A

performs a test on each item in a collection. If it passes the test, then it is moved into a new array and returns only those items

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

What are the 2 ways you can convert a string into a number?

A

parseInt(value) //returns an integer or whole number

parseFloat(value) //returns a decimal value

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

What built in methods come with JavaScript string objects?

A

.length()
.toUpperCase()
.toLowerCase()

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

What are some common methods on the Math object in JavaScript?

A

Math.random() //returns a random number below the number passed

Math.ceil() //returns the highest value of a whole number

Math.floor() //returns the lowest value of a whole number

Math.round() //returns the rounded value

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

How do you get a random number between a range using Math.random()?

A
  1. Build a function that will generate a random number
  2. Pass a max and a min as your parameters
  3. Inside your function, ‘floor’ your result by wrapping it inside Math.floor
  4. Establish a range by subtracting your min from your max, adding 1 and add your min to the end
  5. Multiply your range with Math.random

Math.floor( Math.random( ) * ( max - min + 1 ) + min )

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