Part 1 Flashcards

1
Q

Must you explicitely type variables?

A

No,

var i = 0;

OR

i = 0;

OR

i = { }

which means you can assign them to another type.

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

how is var scoped

A

Function unless there is no function in which case it is global

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

how is let scoped

A

Block as in between the curly braces

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

let i = 10;

if (i == 10) {
 let i = 5;
 console.log(i);
{

console.log (i);

What is the output from this?

A

5
10

because the let in side the curly braces is scoped within the curly braces.

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

Is “let” hoisted inside a function?

A

No, if it is referenced before it is declared, you get an error.

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

Example of Tagged Templates. (note back ticks)

Assume
let harry = ` Mr. and Mrs. Dursley next door `
let fred = ` Mr. and Mrs. Baggins the other next door `
let betty = ` Mr. and Mrs. Bilbo across the street`

let lines = [harry, fred, betty] ;

using a function buildHTML

A

function buildfHTML(strings, expr, expr1, exprLines) {
console.log(strings);
console.log(expr);
console.log(expr1);
console.log(exprLines);
}

const result = buildHTML${ lines[0] }, ${ lines[1] }, ${ exLines }

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

What are REST parameters?

A

indicates the “rest of” the parameters :-)

Input: function example( first, last, …details);

This is the opposite of “Spread” parameters.

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

What are spread parameters?

A

The opposite of REST parameters, the Spread the argument out in a statement as here:

function example(message, ...parms) { \<-- parms is rest here
 console.log(message, ...parms); \<-- parms is spread here
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Show an example of a tail call?

A

If the last line of a function (the return statement) calls yet another function ( or recurses ) and then further manipulates the return from the call before returning, this is not a tail call and requires additional stack for the call.

If it does NOT manipulate the results of the call before returning, TCO (Tail call optimization) just reuses the existing call stack with out any further stack allocation.

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

Given variables a and b and then an array: ara = [‘first’, ‘second”],

show a destructured assignment leaving a = ‘first’, and b= ‘second’.

A

[a, b] = …ara;

OR

[a, b] = [‘first’, ‘second’]

[a, b, …rest] = [‘first, ‘second’, 30, 40, 50];
here rest becomes [30, 40, 50]

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

What happens in ES6+ for the following destructuring of variables from objects?

var course = { name: “French”, publisher: “Louis” };

function crsDetails( course ) {
 let { name, publisher, price } = course;
...
A

ES6+ at least recognizes it as an error since price can’t be set.

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

Show how to mark a parameter is optional in a destructured expression that follows: (price is optional)

let { name, publisher, price } = c;

A

let { name, publisher, ?price } = c;

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

Difference between for .. of and for .. in operators.

A

for …of use to iterate over interables (arrays).

for …in use to iterate over properties of an object.

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

How to retrieve value(s) from a generator such as:

function \* generatorFunction( ) {
 yield 'a';
 yield 'b';
 return 'c';
 yield 'd';
 }

What happens when it is called four times.

A

Calling

var x = generatorFunction( );

will return ‘a’, then ‘b’, then returns with the done property set to true.

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

What type of object is returned by a generator function?

A

{ value: any, done: boolean }

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

let s = new Set( [10, 20, 30] );

s.add( 20 );

What will happen?

A

20 will not be added again because it is already there.

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

How to remove entry ‘20’ from a set ‘s’.

A

s.delete( 20 );

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

Show using a Map iterator to get map entry contents.

A

m.set( ‘0’, ‘foo’ );
m.set( 1, ‘bar’ );
const iterator = m.keys();
console.log(iterator.next().value ); // “0”
console.log(iterator.next().value ); // 1

m.size; // 2

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

Show how to set a map entry.

Console.log( show if value was set );

A
myMap = new Map();
... myMap.set( name**,**'first' );

console.log( myMap.has(keyvalue) ); // true of false

20
Q

show how to empty a map.

A

map.clear( );

21
Q

show how to delete a specific map entry

A

map.delete( key );

22
Q

Show how to get the value of a map given a key

A

myValue = map.get( key );

23
Q

Show how to test for a specific key value in a map.

A

map.has( key );

24
Q

show how to interate over a map and show the key, value, and map.

A

var m = new Map( );

m. set( ‘a’, 1 );
m. set( ‘b’, 2 )

m. forEach ( (k, v, m) =>
console. log( **key: ${ k } value: ${ v } map: ${ m }** ) )

// key:1 value:a map:[object Map]
// key:2 value:b map:[object Map]
25
Q

show using for .. of to iterate a map showing key and value

A

for ( [key,value] of m )
console.log( key + ‘=’ + value )

// a=1
// b=2
26
Q

Show how to use an map iterator to obtain the entries.

A

var m = new Map( )

m. set( ‘a’, 1 )
m. set( ‘b’, 2 )

var iter = m.**keys**( )
obj = iter.next(); // obj: {value: "a", done: false}
obj = iter.next(); // obj: {value: "b", done: false}
obj = iter.next(); // obj: {value: undefined, done: true}
27
Q

Show how to use an iterator with a Map to obtain the values.

A

var m = new Map( )

m. set( ‘a’, 1 )
m. set( ‘b’, 2 )

var iter = m.**values**( )
obj = iter.next(); // obj: {value: "a", done: false}
obj = iter.next(); // obj: {value: "b", done: false}
obj = iter.next(); // obj: {value: undefined, done: true}
28
Q

Describe a MAP function

A

Objects have been used as maps (key value pairs).

Keys can be any type and are placed in the order they were inserted.

Diff between Map and Object

Objects must be string keys, Maps any type
Objects are now ordered, but you must find the keys and then iterate.

Maps have a size function.

Example Map:
let contacts = new Map()
 contacts.set('Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"})
 contacts.has('Jessie') // true
 contacts.get('Hilary') // undefined
 contacts.set('Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}) contacts.get('Jessie') // {phone: "213-555-1234", address: "123 N 1st Ave"}
 contacts.delete('Raymond') // false
 contacts.delete('Jessie') // true
 console.log(contacts.size) // 1
29
Q

Show example of jQuery and AJAX. ( commonly used pre ES6 )

A

$.ajax(“http://services.odata.org/v4/Northwind/Northwind.svc/”, {

success: function(data) {
console. log(data.value);
* *} ,**
error: function( ) {
console. log(“failed to fetch data”) ;
* *}**
* *} ) ;**

30
Q

Show example of using ES6+ fetch method.

A

fetch( “http://services/odata.org/V4/Northwind/Northwind.svc/”, {
method: ‘get’ } ) . then( function(response) {
return response.json() ;
} ) . then( function(data) {
console.log( data.value) ;
} ) . catch( function( ) {
console.log(“failed to fetch data) ;
} ) ;

31
Q

Show a promise constructor.

A

function add_positivenos_async(n1, n2)
{
let p = new Promise(function (resolve, reject) {
if (n1 >= 0 && n2 >= 0) { //do some complex work
resolve(n1 + n2) }
else
reject(‘NOT_Postive_Number_Passed’)

} )
return p;
}

add_positivenos_async(10, 20)

  • *.**then(successHandler) // if promise resolved
  • *.**catch(errorHandler);// if promise rejected

add_positivenos_async(-10, -20)

  • *.**then(successHandler) // if promise resolved
  • *.**catch(errorHandler);// if promise rejected
function errorHandler(err) {
 console.log('Handling error', err)
}
function successHandler(result) {
 console.log('Handling success', result)
}
32
Q

Show using promise to wrap a jQuery Ajax pattern.

A

var promise = new Promise( resolve, reject ) {

$.ajax( “http://services.odata.org/V4/Northwind/Northwind.svc/”, {
success: function (data) {
resolve(data);
} ,
error: function( ) {
reject(“Error”);
}
}

promise.then (function (result) {
console.log( result );
} , function ( err ) {
console.log( err );
} ) ;

33
Q

Show example of string repeat

A

var stg = “SkillBakery “.repeat(2);

stg is “SkillBakery SkillBakery “;

34
Q

String contains has been deprecated. Show how to determine if a string contains a substring.

A

result: bool = “ World “.includes( “rl” ) ;

true

35
Q

What is the method to test the starting of a string.

A

var x:bool = “SkillBakery ES6” . startsWith( “Skill” ) ;

true

36
Q

What is the method to test the ending of a string.

A

var x:bool = “SkillBakery ES6” . endsWith( “Bakery” ) ;

false

37
Q

Iterate over a string.

A

for ( var ch of ‘SkillBakery’) {
console.log( ch ) ;
}

38
Q

Show each:
how to get the part of the number that left of decimal

test if the number IS a number or not

test if a number if finite or not.

A

Math.trunc(39.7) // 39

Number.isNaN(45) // false

Number.isFinite(-Infinity) // false

39
Q

How to search an array of strings for a specific entry?

A

var result = [“skill”, “bakery”, “es6”, “course”]. find(x => x == “Bakery”);

40
Q

Show how to create an Array using the characters in a string.

A

var myA = Array.from(“this is a test”);

41
Q

Show creating an array using passed arguments.

A

var A = Array.of(42, 16, 20);

42
Q

Show how to overwrite an array from a starting position.

A

[0, 0, 0] . fill( 7, 1 ) starts at position 1: [0, 7, 7]

43
Q

Find the index of an entry in an Array.

A

[1, 2, 3, 4, 5] . findIndex(x => x == 2)

result is 1

44
Q

Show how to copy (overwriting) an array within.

A

[1, 2, 3, 4, 5, 6] . copyWithin( 3, 1 )

says find the entry with value ‘3’ and begin copying AFTER that using the source starting with index == 1 (value ‘2’).

result: [1, 2, 3, 2, 3, 4]

45
Q

Show combining one or more source objects.

A
var destination = { start: 0 };
var source\_mid = { interval: 10, duration: 20 } ;
var source\_end = { distance: 40 };

Object.assign( destination, source_mid, source_end ) ;

destination is the concatenation of itself and the other arguments.

subsequent changes to sources, do not affect the new destination entries.

46
Q

What is a “template literal”?

A

ES5 they were called template strings.

They are string literals showing embedded expressions.

string text

string text line 1 string text line 2

string text ${expression} string text

tagstring text ${expression} string text

If there is an expression preceding the template literal (tag here), this is called a tagged template. In that case, the tag expression (usually a function) gets called with the template literal, which you can then manipulate before outputting.