Skip to content

Instantly share code, notes, and snippets.

Chase Isley theonlychase

View GitHub Profile
View sort
// Sort words by a given String
const sortByString = (arr, str) => arr.sort(([a], [b]) => str.indexOf(a) - str.indexOf(b)); // I guess the [a] [b] gives you the first letter of the value
sortByString(["apple", "banana", "cherry", "date"], "dbca") // "date", "banana", "cherry", "apple"]
View create-array
// Arrays are objects with numerical keys and a length property
// FANCY OBJECTS
const arr = []
typeof arr // "object"
const friendsArray = ['Jake', 'Jordyn', 'Mikenzi']
const friendsObj = {0: 'Jake', 1: 'Jordyn', 2: 'Mikenzi'}
friendsArray[1] // Jordyn
friendsObj[1] // Jordyn
View class
// Create Parent Class
class Person {
constructor(first, age) {
this.first = first;
this.age = age;
}
fullName() {
console.log(`${this.first} is ${this.age} years old`)
}
View proto-__proto__
## prototype vs __proto__
1. Two prototypes in Javascript
1. `prototype`
1. Special property assigned to any function you make in JS
2. This `prototype` is the same object that is pointed to by the [[Prototype]] of the newly created object from that function (using `new` keyword)
2. `[[Prototype]]`
1. This property is a reference to the `prototype` of the function from which the object was made
View flatten-array
function flattenArray(array) {
if (arguments.length === 0) return [];
else return Array.from(flatten(array));
}
// Generator
function *flatten(array) {
// loop each value in the array
for (elt of array)
if (Array.isArray(elt)) yield *flatten(elt); // if value is an array it calls itself and passes in the value
View constructor
## Constructor Functions
1. The `new` keyword - *Pseudoclassical Instantiation*
1. when you invoke a function using the `new` keyword, these two lines are done for you implicitly (“under the hood”) and the object that is created is called `this`.
2. If you leave off `new` when you invoke the function, that `this` object never gets created nor does it get implicitly returned.
```javascript
// ES5
function Animal (name, energy) {
// const this = Object.create(Animal.prototype)
View math-methods
// Math.round()
// The Math.round(x) method returns the value of x rounded to the nearest integer.
Math.round(3.4); // 3
Math.round(3.5); // 4
// Math.pow()
// The Math.pow(x, y) method returns the base to the exponent power, that is, baseexponent.
Math.pow(5, 3); // 125
// Math.sqrt()
@theonlychase
theonlychase / isNaN
Last active May 2, 2019
Test if a string is not equal to NaN
View isNaN
// Create a function that splits these strings into their alphabetic and numeric parts.
function splitCode(item) {
let nums = item.split("").filter(val => !isNaN(val)).join("");
let str = item.split("").filter(val => !nums.includes(val)).join("");
return [str, parseInt(nums)]
}
splitCode("TEWA8392")
// NOTE
View arr-subarr
function matrix(x, y, z) {
return [...Array(x)].map(e => Array(2).fill(z));
}
matrix(3, 2, 3) // [[3, 3], [3, 3], [3, 3]]
const matrix = (x, y, z) =>
Array.from({length: x}, () =>
Array.from({length: y}, () => z)
);
View regex
// Add a variable to a Regular expression
function replace(str, r) {
let regex = new RegExp(`[${r}]`, "g");
return str.replace(regex, "#")
}
replace("abcdef", "c-e")
You can’t perform that action at this time.