Week 5 part 2 Flashcards
What are the 3 types of loops JavaScript supports?
while, do-while, for
JavaScript provides how many main ways to define an array? What are they? How many types of arrays are there in JS?
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
To change the 1st element in the cars array what is the syntax
cars[0] = “new”;
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);
}
for (let x of cars) {
console.log(x);
}
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)
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
What is the function expresion?
1 way to define a function
let myFunction = function namedFunction()
When referring to functions, what is the difference between using functionName and functionName()?
functionName will refer to the function object, functionName will refer to the function result
A variable that’s is defined to declared outside of any function is ______
global
What do these do?
Math.round(x)
Math.power(x,y)
Math.sqrt(x)
Math.abs(x)
Math.random()
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.
Filter - a JavaScript array method, what does it do? What do we call it?
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