General Flashcards

1
Q

What is the difference between recursion and iteration?

A

Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false.
The primary difference between recursion and iteration is that is arecursionis a process, always applied to a function. Theiterationis applied to the set of instructions which we want to get repeatedly executed.

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

Key Differences Between Recursion and Iteration

A
  1. Recursion is when a method in a program repeatedly calls itself whereas, iteration is when a set of instructions in a program are repeatedly executed.
    1. A recursive method contains a set of instructions, statement calling itself, and a termination condition whereas iteration statements contain initialization, increment, condition, set of instruction within a loop and a control variable.
    2. A conditional statement decides the termination of recursion and control variable’s value decide the termination of the iteration statement.
    3. If the method does not lead to the termination condition it enters to infinite recursion. On the other hand, if the control variable never leads to the termination value the iteration statement iterates infinitely.
    4. Infinite recursion can lead to system crash whereas, infinite iteration consumes CPU cycles.
    5. Recursion is always applied to method whereas, iteration is applied to a set of instruction.
    6. Variables created during recursion are stored on stack whereas, iteration doesn’t require a stack.
    7. Recursion causes the overhead of repeated function calling whereas, iteration does not have a function calling overhead.
    8. Due to the function calling overhead execution of recursion is slower whereas, execution of iteration is faster.
    9. Recursion reduces the size of code whereas, iterations make a code longer.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why add ‘use strict’?

A

“use strict”

The directive looks like a string: “use strict” or ‘use strict’. When it is located at the top of a script, the whole script works the “modern” way.

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

What can be placed before ‘use strict’?

A

only comments can be placed before use strict

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

Name the different type conversions.

A

String Conversion – Occurs when we output something. Can be performed with String(value). The conversion to string is usually obvious for primitive values.

Numeric Conversion – Occurs in math operations. Can be performed with Number(value).

Boolean Conversion – Occurs in logical operations. Can be performed with Boolean(value).

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

What is a binary operator ? whats a unary operator?

A

An operator is unary if it has a single operand. For example, the unary negation - reverses the sign of a number:

let x = 1;
x = -x;
alert( x ); // -1, unary negation was applied

An operator is binary if it has two operands. The same minus exists in binary form as well:

let x = 1, y = 3;
alert( y - x ); // 2, binary minus subtracts values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Shorter numberic conversion

A

Numeric conversion, unary +.
alert( +true ); // 1
alert( +”” ); // 0

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

The difference between function declaration and function expressions?

A

Function Declaration: a function, declared as a separate statement, in the main code flow.

// Function Declaration
function sum(a, b) {
  return a + b;
}
Function Expression: a function, created inside an expression or inside another syntax construct. Here, the function is created at the right side of the “assignment expression” =:
// Function Expression
let sum = function(a, b) {
  return a + b;
};
The more subtle difference is when a function is created by the JavaScript engine.

A Function Expression is created when the execution reaches it and is usable only from that moment.

Once the execution flow passes to the right side of the assignment let sum = function… – here we go, the function is created and can be used (assigned, called, etc. ) from now on.

Function Declarations are different.

A Function Declaration can be called earlier than it is defined.

Another special feature of Function Declarations is their block scope.

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

What are linters?

A

Linters are tools that can automatically check the style of your code and make improving suggestions.
• JSLint– one of the first linters.
• JSHint– more settings than JSLint.
• ESLint– probably the newest one.

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

What is Babel?

A

When we use modern features of the language, some engines may fail to support such code. Just as said, not all features are implemented everywhere.

Here Babel comes to the rescue.

Babel is a transpiler. It rewrites modern JavaScript code into the previous standard.

Actually, there are two parts in Babel:

First, the transpiler program, which rewrites the code. The developer runs it on their own computer. It rewrites the code into the older standard. And then the code is delivered to the website for users. Modern project build systems like webpack provide means to run transpiler automatically on every code change, so that it’s very easy to integrate into development process.

Second, the polyfill.

New language features may include new built-in functions and syntax constructs. The transpiler rewrites the code, transforming syntax constructs into older ones. But as for new built-in functions, we need to implement them. JavaScript is a highly dynamic language, scripts may add/modify any functions, so that they behave according to the modern standard.

A script that updates/adds new functions is called “polyfill”. It “fills in” the gap and adds missing implementations.

Two interesting polyfills are:

core js that supports a lot, allows to include only needed features.
polyfill.io service that provides a script with polyfills, depending on the features and user’s browser.
So, if we’re going to use modern language features, a transpiler and a polyfill are necessary.

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

What is the difference between transpiler and compiler?

A

A source-to-source translator, source-to-source compiler (S2S compiler), transcompiler or transpiler[1][2] is a type of translator that takes the source code of a program written in a programming language as its input and produces an equivalent source code in the same or a different programming language. A source-to-source translator converts between programming languages that operate at approximately the same level of abstraction, while a traditional compiler translates from a higher level programming language to a lower level programming language. For example, a source-to-source compiler may perform a translation of a program from Python to JavaScript, while a traditional compiler translates from a language like C to assembler or Java to bytecode

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

What types of Numbers do we have?

A

In modern JavaScript, there are two types of numbers:

Regular numbers in JavaScript are stored in 64-bit format IEEE-754, also known as “double precision floating point numbers”. These are numbers that we’re using most of the time, and we’ll talk about them in this chapter.

BigInt numbers, to represent integers of arbitrary length. They are sometimes needed, because a regular number can’t exceed 253 or be less than -253. As bigints are used in few special areas, we devote them a special chapter BigInt.

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

Provide details about regular numbers.

A

They are 64 bit, range from 2tothepowerof 53 to -2**53, 52 of them are used to store the digits, 11 of them store the position of the decimal point (they are zero for integer numbers), and 1 bit is for the sign.

for large numbers we use e or e-
1e-3 = 1 / 1000 (=0.001)

// -6 divides by 1 with 6 zeroes
1.23e-6 = 1.23 / 1000000 (=0.00000123),

We can have hex, binary and octal numbers:
alert( 0xff ); // 255 hexadecimal
let a = 0b11111111; // binary form of 255
let b = 0o377; // octal form of 255

alert( a == b ); // true, the same number 255 at both sides

There are only 3 numeral systems with such support. For other numeral systems, we should use the function parseInt

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

What does num.toString(base) do?

A

The method num.toString(base) returns a string representation of num in the numeral system with the given base.

For example:

let num = 255;

alert( num.toString(16) ); // ff
alert( num.toString(2) ); // 11111111
The base can vary from 2 to 36. By default it’s 10.

Common use cases for this are:

base=16 is used for hex colors, character encodings etc, digits can be 0..9 or A..F.

base=2 is mostly for debugging bitwise operations, digits can be 0 or 1.

base=36 is the maximum, digits can be 0..9 or A..Z. The whole latin alphabet is used to represent a number. A funny, but useful case for 36 is when we need to turn a long numeric identifier into something shorter, for example to make a short url. Can simply represent it in the numeral system with base 36:

alert( 123456..toString(36) ); // 2n9c

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

When do we use two dots to call a method?

A

Please note that two dots in 123456..toString(36) is not a typo. If we want to call a method directly on a number, like toString in the example above, then we need to place two dots .. after it.

If we placed a single dot: 123456.toString(36), then there would be an error, because JavaScript syntax implies the decimal part after the first dot. And if we place one more dot, then JavaScript knows that the decimal part is empty and now goes the method.

Also could write (123456).toString(36).

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

What are the rounding functions we can use with numbers?

A

There are several built-in functions for rounding:
Math.floor
Rounds down: 3.1 becomes 3, and -1.1 becomes -2.
Math.ceil
Rounds up: 3.1 becomes 4, and -1.1 becomes -1.
Math.round
Rounds to the nearest integer: 3.1 becomes 3, 3.6 becomes 4 and -1.1 becomes -1.
Math.trunc (not supported by Internet Explorer)
Removes anything after the decimal point without rounding: 3.1 becomes 3, -1.1 becomes -1.

toFixed(n) rounds the number to n digits after the point and returns a string representation of the result.

let num = 12.34;
alert( num.toFixed(1) ); // "12.3"
This rounds up or down to the nearest value, similar to Math.round:
let num = 12.36;
alert( num.toFixed(1) ); // "12.4"

Please note that result of toFixed is a string. If the decimal part is shorter than required, zeroes are appended to the end:

let num = 12.34;
alert( num.toFixed(5) ); // "12.34000", added zeroes to make exactly 5 digits. Please note that toFixed always returns a string. 
We can convert it to a number using the unary plus or a Number() call: +num.toFixed(5).
17
Q

What does NaN represent?

A

It represents an error.
They belong to the type number, but are not “normal” numbers, so there are special functions to check for them:

isNaN(value) converts its argument to a number and then tests it for being NaN:

alert( isNaN(NaN) ); // true
alert( isNaN(“str”) ); // true
But do we need this function? Can’t we just use the comparison === NaN? Sorry, but the answer is no. The value NaN is unique in that it does not equal anything, including itself:

alert( NaN === NaN ); // false

18
Q

What does isFinite(value) do?

A

isFinite(value) converts its argument to a number and returns true if it’s a regular number, not NaN/Infinity/-Infinity:

alert( isFinite(“15”) ); // true
alert( isFinite(“str”) ); // false, because a special value: NaN
alert( isFinite(Infinity) ); // false, because a special value: Infinity
Sometimes isFinite is used to validate whether a string value is a regular number:

let num = +prompt(“Enter a number”, ‘’);

// will be true unless you enter Infinity, -Infinity or not a number
alert( isFinite(num) );
Please note that an empty or a space-only string is treated as 0 in all numeric functions including isFinite.
19
Q

What does object.is() do?

A

There is a special built-in method Object.is that compares values like ===, but is more reliable for two edge cases:

It works with NaN: Object.is(NaN, NaN) === true, that’s a good thing.
Values 0 and -0 are different: Object.is(0, -0) === false, technically that’s true, because internally the number has a sign bit that may be different even if all other bits are zeroes.
In all other cases, Object.is(a, b) is the same as a === b.

This way of comparison is often used in JavaScript specification. When an internal algorithm needs to compare two values for being exactly the same, it uses Object.is (internally called SameValue).

20
Q

Name the numeric conversions:

A

1) Numeric conversion using a plus + or Number() is strict. If a value is not exactly a number, it fails:
alert( +”100px” ); // NaN
The sole exception is spaces at the beginning or at the end of the string, as they are ignored.
They “read” a number from a string until they can’t. In case of an error, the gathered number is returned. The function parseInt returns an integer, whilst parseFloat will return a floating-point number:

alert( parseInt(‘100px’) ); // 100
alert( parseFloat(‘12.5em’) ); // 12.5

alert( parseInt(‘12.3’) ); // 12, only the integer part is returned
alert( parseFloat(‘12.3.4’) ); // 12.3, the second point stops the reading
There are situations when parseInt/parseFloat will return NaN. It happens when no digits could be read:

alert( parseInt(‘a123’) ); // NaN, the first symbol stops the process