Basics Flashcards

1
Q

Arrow functions

A

let addTwoNums = (n1,n2) => n1+n2;

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

forEach in array

A
let nums = [10, 20, 30];
nums.forEach((number, index, array) => {
console.log(number)
});
Note: number is an element of array, 
index and array are optional.

nums.forEach(function(number) {
console.log(number);
});

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

For - of: iterate over an array

A

for (const myElement of myArray) {
console.log(myElement);
}

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

Add an element to an array

A

PUSH(): add an element to at the END of an array

UNSHIFT(): adds it at the BEGINNING of an array

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

Removal elements from arrays

A

POP(): remove the last element of an array

SPLICE(index from which to begin removing, number of elements to remove)

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

Sort()

A
const numbers =[3, 23,12]
let sorted = numbers.sort((a,b) => a-b); // 3,12,23

let sorted_desc = numbers.sort((a, b) => b-a); // 23, 12, 3

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

filter()

Returns a new array

A

let cities = [
{ name: “Los Angeles”, population: 3700000 },
{ name: “New York”, population: 8175133 },
{ name: “Chicago”, population: 2695598 },
{ name: “Houston”, population: 2099451 },
{ name: “Philadelphia”, population: 1526006 },
];

// FILTER() returns a new array with all elements that pass the test implemented by the callback() function
// eg: arrayObject.filter(callback, contextObject);
// another way using filter()
let bigCities = cities.filter((city) => city.population > 3000000);
console.log(bigCities1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Reduce()

A

// acc: accumulator; c: current value; 0: starting value of accumulator

let numArr = [11, 35, 67, 14, 78, 10];
let sum = numArr.reduce((acc, c) => acc + c, 0);
console.log(sum);

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

In JavaScript, a function is a value, so we can deal with it as a value.

A
function sayHi() {
  alert( "Hello" );
}

alert( sayHi ); // shows the function code

Please note that the last line does not run the function, because there are no parentheses after sayHi. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
function sayHi() {   // (1) create
  alert( "Hello" );
}

let func = sayHi; // (2) copy

func(); // Hello     // (3) run the copy (it works)!
sayHi(); // Hello    //     this still works too (why wouldn't it)
A

The Function Declaration (1) creates the function and puts it into the variable named sayHi.
Line (2) copies it into the variable func. Please note again: there are no parentheses after sayHi. If there were, then func = sayHi() would write the result of the call sayHi() into func, not the function sayHi itself.
Now the function can be called as both sayHi() and func().

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

Array constructor with multiple parameters

A

let fruits = new Arrat(‘apple’, ‘banana’);

console. log(fruits.length); //2
console. log(fruits[0]); //‘apple’

Example 2:

let colors = new Array(2);

console. log(colors.length); //2
console. log(colors[0]); //undefined

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

Linking to Same Page in HTML

A

In order to link to a target on the same page, we must give the target an ID.
EXAMPLE:

<p>This is the top of the page</p>

<p></p>

<h1>This is the bottom!</h1>

The target link is a string containing the octothorpe # character and the target element’s id.

<ol>
<li><a>Top</a></li>
<li><a>Bottom</a> </li></ol>

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

Linking to relative page.
Files are store on the same folder-> can use a relative path
Eg: ./index.html

A

<a>Contact</a>

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