Skip to content

Instantly share code, notes, and snippets.

@pillows
Created May 30, 2019 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pillows/b8b2e003b379e63f28de9f6cf7f0b8ab to your computer and use it in GitHub Desktop.
Save pillows/b8b2e003b379e63f28de9f6cf7f0b8ab to your computer and use it in GitHub Desktop.
TTP Javascript Recreating Prototype Assignment
var array1 = ["Apple", "Orange", "Pear", "Lemon"]
let myEach = (array, callback) => {
for(let i = 0; i < array.length; i++){
callback(array[i])
}
}
// myEach(array1,function(element){
// console.log(element)
// });
let myMap = (array, callback) => {
let newArray = []
for(let i = 0; i < array.length; i++){
newArray[i] = callback(array[i]);
}
return newArray
}
// const testMap = myMap(array1, element => element + "2x")
//
// console.log(testMap)
let myFilter = (array, callback) => {
let newArray = []
for(let i = 0; i < array.length; i++){
if(callback(array[i])){
newArray.push(array[i])
}
}
return newArray
}
// const filter = myFilter(array1, element => element.length < 5)
// console.log(filter)
let mySome = (array, callback) => {
for(let i = 0; i < array.length; i++){
if(callback(array[i]))
return true
}
return false
}
// const some = mySome(array1, element => element.length < 5)
// console.log(some)
let myEvery = (array, callback) => {
let flag = false
if(array.length === 0)
return true
for(let i = 0; i < array.length; i++){
if(callback(array[i]))
flag = true
else {
flag = false
}
}
return flag
}
// const every = myEvery(array1, element => element.length < 5)
// console.log(every)
let myReduce = (array, callback) => {
let result;
for(let i = 0; i < array.length; i++){
result = callback(result,array[i])
}
return result
}
// Not sure why there's an undefined here?
// const reducer = (accumulator, currentValue) => accumulator + currentValue
// const reduceResult = myEvery(array1, reducer)
// console.log(reduceResult)
let myIncludes = (array, callback) => {
for(let i = 0; i < array.length; i++){
if(array[i] === callback)
return true
}
return false;
}
// const includes = myIncludes(array1, "Lemon")
// console.log(includes)
let myIndexOf = (array, element) => {
for(let i = 0; i < array.length; i++){
if(array[i] === element)
return i
}
return -1
}
// let indexof = myIndexOf(array1, "Lemon")
// console.log(indexof)
// indexof = myIndexOf(array1, "emon")
// console.log(indexof)
let myPush = (array, elementToAdd) => {
array[array.length] = elementToAdd
}
// myPush(array1, "test")
// console.log(array1)
let myLastIndexOf = (array, element) => {
let lastIndex = -1
for(let i = 0; i < array.length; i++){
if(array[i] === element)
lastIndex = i
}
return lastIndex
}
console.log(myLastIndexOf(array1, "Apple"))
let user = {
name: "John",
age: 30,
isAdmin: true
};
let grabValues = (kv) => {
let result = []
for(val in kv){
result.push(val)
}
return result
}
grabValues(user)
let grabKeys = (kv) => {
let result = []
for(val in kv){
result.push(kv[val])
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment