Methods Flashcards

1
Q

Array.prototype.shift()

A

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

var a = [1, 2, 3];
var b = a.shift();

console. log(a); // [2, 3]
console. log(b); // 1

arr.shift()

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

Array.prototype.slice()

A

The slice() method returns a new array object selected from begin to end (end not included). The original array will not be modified.

arr. slice()
arr. slice(begin)
arr. slice(begin, end)

var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
var citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Array.prototype.splice()

A
The splice() method changes the contents of an array by removing existing elements and/or adding new elements.
Return value:
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

Syntax

array. splice(start)
array. splice(start, deleteCount)
array. splice(start, deleteCount, additem1, additem2, …)

if deleteCount is 0 then no elements are removed from the array

var myFish = [‘angel’, ‘clown’, ‘mandarin’, ‘sturgeon’];

myFish.splice(2, 0, 'drum'); 
// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
myFish.splice(2, 1); 
// myFish is ["angel", "clown", "sturgeon"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

for…in

A

The for…in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed

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

Object.prototype.constructor

A

Returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function’s name. The value is only read-only for primitive values such as 1, true and “test”.

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

Math.floor()

A

The Math.floor() function returns the largest integer less than or equal to a given number.

Math.floor( 45.95); //  45
Math.floor( 45.05); //  45
Math.floor(  4   ); //   4
Math.floor(-45.05); // -46 
Math.floor(-45.95); // -46
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Math.pow()

A

The Math.pow() function returns the base to the exponent power, that is, base**exponent.

Syntax
Math.pow(base, exponent)

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

Array.prototype.map()

A
The map() method creates a new array with the results of calling a provided function on every element in this array.
Return value:
A new array with each element being the result of the callback function.
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Number.prototype.toString()

A

The toString() method returns a string representing the specified Number object.

console. log(count.toString()); // displays ‘10’
console. log((17).toString()); // displays ‘17’
console. log((17.2).toString()); // displays ‘17.2’

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

toUpperCase()

A

The toUpperCase() method returns the value of the string converted to upper case. toUpperCase() does not affect the value of the string itself.

Return value
A new string representing the calling string converted to upper case.

console.log(‘alphabet’.toUpperCase()); // ‘ALPHABET’

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

Math.random()

A

The Math.random() function returns a random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

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

Array.prototype.every()

A

The every() method tests whether all elements in the array pass the test implemented by the provided function.

function isBigEnough(element, index, array) { 
  return element >= 10; 
} 

[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true

Syntax
arr.every(callback[, thisArg])

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

Array.prototype.some()

A

The some() method tests whether some element in the array passes the test implemented by the provided function.

function isBiggerThan10(element, index, array) {
  return element > 10;
}

[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true

Syntax
arr.some(callback[, thisArg])

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

String.prototype.indexOf()

A

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.

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

charCodeAt()

A

var str = “HELLO WORLD”;
var n = str.charCodeAt(0);
The result of n will be:
72

to use this properly:
.toLowerCase() on the string
str.charCodeAt(x) - 96;

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

String.prototype.substr()

A

The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

Syntax
str.substr(start [, length])
Parameters

start
Location at which to begin extracting characters. If a negative number is given, it is treated as strLength + start where strLength is the length of the string. For example, str.substr(-3) is treated as str.substr(strLength - 3)
length
Optional. The number of characters to extract.
Return value
length counts the index of start

A new string containing the extracted section of the given string. If length is 0 or a negative number, an empty string is returned.

17
Q

String.prototype.substring()

A

The substring() method returns a subset of a string between one index and another, or through the end of the string.

Syntax
str.substring(indexStart[, indexEnd])