Basic Javascript Flashcards

1
Q

arr.unshift(element1[, …[, elementN]])

A

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

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

arr.shift()

A

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

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

arr.push(element1[, …[, elementN]])

A

The push() method adds one or more elements to the end of an array and returns the new length of the array.

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

arr.pop()

A

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

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

undefined

A

undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.

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

null

A

The value null represents the intentional absence of any object value. It is one of JavaScript’s primitive values and is treated as falsy for boolean operations.

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

falsy

A

A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.

if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

NaN

A

The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.

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

parameter

A

Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or “passed”) into a function when it is called are known as arguments.

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

argument

A

An argument is a value (primitive or object) passed as input to a function.

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

differences between parameter & argument

A

Function parameters are the names listed in the function’s definition.

Function arguments are the real values passed to the function.

Parameters are initialized to the values of the arguments supplied.

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

let

A

The let statement declares a block scope local variable, optionally initializing it to a value.

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

const

A

Constants are block-scoped, much like variables defined using the let keyword. The value of a constant can’t be changed through reassignment, and it can’t be redeclared.

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

var

A

The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.

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

Math.random()

A

The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Math.random() * (max - min) + min;

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

global scope

A

In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.

Variables which are used without the var keyword are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with var.

17
Q

local scope

A

Variables which are declared within a function, as well as the function parameters have local scope. That means, they are only visible within that function.

18
Q

!==

A

The strict inequality operator (!==) is the logical opposite of the strict equality operator. It means “Strictly Not Equal” and returns false where strict equality would return true and vice versa.

Strict inequality will not convert data types like !=

19
Q

break;

A

The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

break [label];
label Optional
Identifier associated with the label of the statement. If the statement is not a loop or switch, this is required.

20
Q

switch () {
case (value):

A

The switch statement evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

switch (expression) {
  case value1:
    //Statements executed when the
    //result of expression matches value1
    [break;]
  case value2:
    //Statements executed when the
    //result of expression matches value2
    [break;]
  ...
  case valueN:
    //Statements executed when the
    //result of expression matches valueN
    [break;]
  [default:
    //Statements executed when none of
    //the values match the value of the expression
    [break;]]
}
21
Q

Object.keys(obj)

A

The Object.keys() method returns an array of a given object’s own enumerable property names, iterated in the same order that a normal loop would.

22
Q

how to access the properties of an object

A

There are two ways to access the properties of an object: dot notation (.) and bracket notation ([””]), similar to an array.

Dot notation is what you use when you know the name of the property you’re trying to access ahead of time.

23
Q

updating or adding new object properties is the same method

A
object.key = "value" 
object["key"] = "value"
24
Q

delete properties from an object

A

delete object.key

25
Q

accessing object properties with variables

A
var testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};
var playerNumber = 16;  
var player = testObj[playerNumber];
26
Q

problem: counting cards

A

Count Change Cards
+1 2, 3, 4, 5, 6
0 7, 8, 9
-1 10, ‘J’, ‘Q’, ‘K’, ‘A’

function cc(card) {
  var regex = /[JQKA]/;
  if (card > 1 &amp;&amp; card < 7) {
    count++;
  } else if (card === 10 || String(card).match(regex)) {
    count--;
  }

if (count > 0) return count + “ Bet”;
return count + “ Hold”;
}

27
Q

problem: using objects for lookups

A
function phoneticLookup(val) {
  var result = "";
  var lookup = {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
  };
// After converting our case statements into object properties you can make use of the variable `result` to let the function return the correct value.
  result = lookup[val];
  // Only change code above this line
  return result;
}
28
Q

testing objects for properties

A

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.

function checkObj(obj, checkProp) {
  if(obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";
  }
}
29
Q

string.match(regexp)

A

The match() method retrieves the result of matching a string against a regular expression.

30
Q

regex

regular expression

A

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), search(), and split() methods of String.

31
Q

how to iterate over an object

A

for (variable in object)

statement

32
Q

recursive version of multiply

A

multiply(arr, n - 1) * arr[n - 1]
replaces loop

The recursive version of multiply breaks down like this. In the base case, where n <= 0, it returns 1. For larger values of n, it calls itself, but with n - 1. That function call is evaluated in the same way, calling multiply again until n <= 0. At this point, all the functions can return and the original multiply returns the answer.

Note: Recursive functions must have a base case when they return without calling the function again (in this example, when n <= 0), otherwise they can never finish executing.

33
Q

Ternary

A

The conditional operator, also called the ternary operator, can be used as a one line if-else expression.

condition ? statement-if-true : statement-if-false;

34
Q

Using unshift to add element at given index

A
function applyUnshiftAgain(arrayOfArrays, index, element) {
  // add the element to the front of the inner array within the array of arrays located at the index
  arrayOfArrays[index].unshift(element);
  // return the array of arrays
  return arrayOfArrays;

}

35
Q

Array.isArray(value)

A

The Array.isArray() method determines whether the passed value is an Array.

36
Q

Number.parseFloat(value)

A

The Number.parseFloat() method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN.

37
Q

substring

A

The substring() method returns the part of the string between the start and end indexes, or to the end of the string.

str.substring(indexStart[, indexEnd])

38
Q

find the the index of value within a string

A

str.indexOf(searchValue [, fromIndex])

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.