Skip to content

GabyC0/Methods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 

Repository files navigation

Methods

map()

Description of method:

- Creates a new array with the results of calling a function for every array element. It calls the provided function once for each element in an array, in order.

How it works:

- A provided function is called on every element in the array that it's being called on. There is then a new array populated with the results from calling the function.

Syntax:

array.map(function(currentValue, index, arr), thisValue)

Time Complexity:

O(n)

Example:

const nums = [10, 40, 62]
const newNums = nums.map(theFunc)

function theFunc(num) {
  return num + 100;
}

Example:

const location = [
  { trip: 'Mexico', price: 250 },
  { trip: 'Jordan', price: 875 },
  { trip: 'Japan', price: 900 },
  { trip: 'Denmark', price: 700 }
 ]

const locationName = location.map((item) => {
  return location.trip
  return location.price
 })

Example:

const numbers = [4, 2, 7, 10]
const newArr = numbers.map(function(num) {
  return num * 5
})

reduce()

Description of method:

- Reduces the array to a single value. It executes a provided function for each value of an array from left to right. The returned value of the function is stored in an accumulator.

How it works:

- Provide a reducer function that will be executed on each element of an array to produce a single output value.

Syntax:

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

Time Complexity:

O(n)

Example:

const arr = [4, 9, 15, 22]
const reducer = (accumulator, currentVal) {
  return accumulator + currentVal
}, 1)

Example:

const dollars = [2.5, 10.25, .75];
const sum = dollars.reduce( function(total, amount){
  return total + amount
});

sum //13.5

Example:

let val = 8
let sum = [{x: 4}, {x: 7}, {x: 1}].reduce(
  (accumulator, currentVal) => accumulator + currentVal.x
  , val)

filter()

Description of method:

- Creates an array filled with all array elements that pass a test provided as a function.

How it works:

- Creates a new array with all elements that pass the test implemented by the provided function.

Syntax:

array.filter(function(currentValue, index, arr), thisValue)

Time Complexity:

O(n)

Example:

const ages = [18, 25, 15, 33];

function isAdult(age) {
  return age >= 18;
 }

Example:

let books = [
  {name: 'Catcher in the rye', pages: 277},
  {name: 'The House of Spirits', pages: 496},
  {name: 'To Kill a Mockingbird', pages: 281},
  {name: 'The Universe and the Teacup', pages: 227}
  
  let bigBooks = books.filter(page) {
    return page >= 250;
 }

Example:

function positiveNum(value) {
  return value > 0;
}

var filtered = [0, 333, 80, 152, 5].filter(posNum);
print(filtered);

forEach()

Description of method:

- Executes a provided function once for each array element.

How it works:

- Takes the provided function and calls it once for each element in order.

Syntax:

array.forEach(function(currentValue, index, arr), thisValue)

Time Complexity:

O(n)

Example:

let colors = ["black", "yellow", "red", "green", "aqua"];

function colorsAndIndex (item, index){
console.log(`${item} has index ${index}`);
}
colors.forEach(colorsAndIndex);

Example:

function sum() {
  let items = [15, 33, 50];
  let copy = [];

  itms.forEach(function (item) {
    copy.push(item + item+5);
  });
 
 document.write(copy);
}
 sum();

Example:

const items = [15, 33, 50];
let copy = [];

items.forEach(function(item){
  copy.push(item*item);
});
print(copy);

sort()

Description of method:

- Sorts items of an array either alphabetically or numerically, and either ascending or descending.

How it works:

Syntax:

array.sort(compareFunction)

Time Complexity:

O(nlog(n))

Example:

let famMembers = ['Jane', 'Cesar', 'Ulises', 'Maria', 'Ella'];
famMembers.sort();

Example:

let nums = [5, 21, 36, 9];
nums.sort();

Example:

let nums = [5, 21, 36, 9];
nums.sort(function(a, b){return a-b});

slice()

Description of method:

- Extracts a section of a string and retuns it as a new string, without modifiying the original string.

How it works:

Syntax:

array.slice(start, end)

Time Complexity:

O(n)

Example:

let colors = ['black', 'blue', 'teal', 'coral', 'red'];

colors.slice(0,2);

Example:

let nums = [7, 83, 39, 90, 6];

let newNums = nums.slice(3);

Example:

let users = ['Mary', 'Oliver', 'Bob', 'Gabe'];

users.slice(1, 3);

pop()

Description of method:

- Removes the last element of an array, and returns that element.

How it works:

Syntax:

array.pop()

Time Complexity:

O(1)

Example:

let iceCreamFlavors = ['strawberry', 'vanilla', 'chocolate', 'pecan', 'mocha'];

function myIceCreamOptions() {
  iceCreamFlavors.pop();

Example:

```js let numbers = [7, 4, 5, 67, 20, 46];

function nums() { numbers.pop();

<h3>Example:</h3>

```js

shift()

Description of method:

- Removes the first item of an array.

How it works:

Syntax:

``` array.shift() ```

Time Complexity:

O(n)

Example:

```js
<h3>Example:</h3>

```js

Example:

push()

Description of method:

- Adds new items to the end of an array, and returns the new length.

How it works:

Syntax:

array.push(item1, item2, ..., itemX)

Time Complexity:

O(1)

Example:

Example:

Example:

unshift()

Description of method:

- Adds new items to the beginning of an array, and returns the new length.

How it works:

Syntax:

array.unshift(tem1, item2, ..., itemX)

Time Complexity:

O(n)

Example:

Example:

Example:

includes()

Description of method:

- Determines whether a string contains the characters of a specified string. The method returns true if the string contains the characters and, false if not.

How it works:

Syntax:

string.includes(searchvalue, start)

Time Complexity:

O(n)

Example:

Example:

Example:

indexOf()

Description of method:

- Searches the array for the specified item and returns its position. The search will start at the specified position or at the beginning if no start position is specified. (Returns -1 if the item is not found.

How it works:

Syntax:

array.indexOf(item, start)

Time Complexity:

O(n)

Example:

```js
<h3>Example:</h3>
```js

Example:

```js ```

every()

Description of method:

- Tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value)

How it works:

Syntax:

array.every(function(currentValue, index, arr), thisValue)

Time Complexity:

O(n)

Example:

Example:

```js
<h3>Example:</h3>
```js

charAt()

Description of method:

- Returns the character at the specified index in a sting.

How it works:

It outputs a character depending on the index of the value. The index of the first character is 0, the next is 1, then 2, and so on.

Syntax:

public char charAt(int index)

Time Complexity:

O(n)

Example:

Example:

Example:

charCodeAt()

Description of method:

- Returns the Unicode of the character at the specified index in a string.

How it works:

You specify an index and the method returns an integer between 0 and 65535 representing the UTF- 16 code unit at the given index.

Syntax:

string.charCodeAt(index)

Time Complexity:

O(n)

Example:

Example:

Example:

concat()

Description of method:

- Used to join two or more strings. It does not change the existing strings, instead, it returns a new string containing the text of the joined strings.

How it works:

If you have multiple strings and want to join them yet not change them then you can concatenate them by using the concat method.

Syntax:

string.concat(string1, string2, ..., stringX)

Time Complexity:

O(n)

Example:

Example:

Example:

includes()

Description of method:

- Determines whether an array includes a certain value among its entries, returning true or false as appropriate.

How it works:

Look for a value within an array. If the value is withing the array it will return true, if not, will return false.

Syntax:

includes(searchElement)
includes(searchElement, fromIndex)

Time Complexity:

O(n)

Example:

Example:

Example:

indexOf()

Description of method:

- Returns the first index at which a given element can be found in the array, or -1 if it is not present.

How it works:

The method will return the position of the first occurrence of a specified value in a string. If the value cannot be found then the return will be -1.

Syntax:

string.indexOf(searchvalue, start)

Time Complexity:

O(n)

Example:

Example:

Example:

match()

Description of method:

- Searches a string for a match against a regular expression, and returns the matches, as an Array object.

How it works:

The method retrieves the result by matching a string against the regular expression to return a new array.

Syntax:

string.match(regexp)

Time Complexity:

O(n)

Example:

```js ```

Example:

```js ```

Example:

```js ```

repeat()

Description of method:

- Constructs and returns a new sting which contains the specified number of copies of the string on which it was called, concatenated together.

How it works:

The method returns a new string with a specified number of copies of the string it was called on.

Syntax:

string.repeat(count)

Time Complexity:

O(n)

Example:

Example:

Example:

replace()

Description of method:

- Returns a new string with some or all matches of a pattern replaced by a string or a function to be called for each match. (If a pattern is a string, only the first occurrence will be replaced.

How it works:

Replaces a specified phrase with another specified phrase.

Syntax:

string.replace(oldvalue, newvalue, count)

Time Complexity:

O(n)

Example:

Example:

Example:

search()

Description of method:

- Searches a string or a regular expression for a specified value, and returns the position of the match or -1 if a match is not found.

How it works:

The method executes a search for a match between a regular expression and the string object.

Syntax:

string.search(searchvalue)

Time Complexity:

O(n)

Example:

Example:

Example:

splice()

Description of method:

- Adds/removes items to/from an array, and returns the removed item(s).

How it works:

The method changes the contents of an array by removing or replacing the existing elements and/or adding new elements in it's place.

Syntax:

array.splice(index, howmany, item1, ....., itemX)

Time Complexity:

O(n)

Example:

Example:

Example:

split()

Description of method:

- Splits a string into an array of substrings, and returns the new array.

How it works:

The method divides a string in an ordered list of substrings and puts them into an array. The division is done by searching for a pattern.

Syntax:

string.split(separator, limit)

Time Complexity:

O(n)

Example:

Example:

Example:

substr()

Description of method:

- Returns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.

How it works:

The method extracts part of a string, beginning at the character of the specified position, and returns the specified number of characters.

Syntax:

string.substr(start, length)

Time Complexity:

O(n)

Example:

Example:

Example:

toLowerCase()

Description of method:

- Converts a string to lowercase letters

How it works:

Returns the calling string value converted to lowercase

Syntax:

string.toLowerCase()

Time Complexity:

Example:

Example:

Example:

toUpperCase()

Description of method:

- Converts a string to uppercase letters.

How it works:

The method returns the calling string value converted to uppercase.

Syntax:

string.toUpperCase()

Time Complexity:

O(n)

Example:

Example:

Example:

trim()

Description of method:

- Removes whitespace from both sides of a string.

How it works:

The method removes white space from the ends including all the whitespace characters such as space, tab, no-break space, etc.

Syntax:

string.trim()

Time Complexity:

Example:

Example:

Example:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published