Week 5 part 2 Flashcards

1
Q

What are the 3 types of loops JavaScript supports?

A

while, do-while, for

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

JavaScript provides how many main ways to define an array? What are they? How many types of arrays are there in JS?

A

2 ways.
Object literal notation: let years = [1855, 1648, 1420]

The Array constructor: const cars = new Array(“Saab”, “Volvo”, “BMW”)

There’s single and multi-dimensional arrays in Java

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

To change the 1st element in the cars array what is the syntax

A

cars[0] = “new”;

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

How can we functionally create an equivalent loop using for… of?
for (let i = 0; i < cars.length; i++) {
let x = years[i];
console.log(x);
}

A

for (let x of cars) {
console.log(x);
}

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

Explain how the splice method works when used on an array

var nums = [2, 4, 6, 8, 10]
nums.splice(3)
nums.splice(0,0,3,5)

A

Splice adds or removes elements from anywhere in the array and returns the deleted elements [important]
splice(startingIndex, totalElementsToDelete, valuesToAdd)

nums.splice(3) // nums = [2 ,4, 6]
nums.splice(0,0,3,5) // nums = [3, 5, 6]

Why the 2nd one is like that I don’t know it doesn’t make sense

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

What is the function expresion?

A

1 way to define a function
let myFunction = function namedFunction()

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

When referring to functions, what is the difference between using functionName and functionName()?

A

functionName will refer to the function object, functionName will refer to the function result

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

A variable that’s is defined to declared outside of any function is ______

A

global

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

What do these do?

Math.round(x)
Math.power(x,y)
Math.sqrt(x)
Math.abs(x)
Math.random()

A

round - returns the value of x rounded to its nearest integer
pow -returns the value of x to the power of y
sqrt - returns the square root of x
abs - returns the absolute positive value of x
random - returns a random number been 0 (inclusive) and 1 (exclusive)

Math has many other functions such as - sin, cos, tan, floor, ceiling, log, exp, etc.

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

Filter - a JavaScript array method, what does it do? What do we call it?

A

Creates a new array based on whether or not elements pass a test provided by a function - We call it a callback function

Check slides 60 & 61

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