eng
competition

Text Practice Mode

underpants

created Jan 24th 2023, 21:06 by


1


Rating

1278 words
1 completed
00:00
_.identity = function(value){
    //  return value
    return value;
}
 
_.typeOf = function(value){
    if (typeof value === "string"){ //  tests if value is a string
        return "string";    //  returns "string"
    }   else if (typeof value === "boolean"){   //  tests if value is a boolean
        return "boolean";   //  returns "boolean"
    }   else if (typeof value === "number"){    //  tests if value is a number
        return "number";    //  returns "number"
    }   else if (typeof value === "undefined"){ //  tests if value is undefined
        return "undefined"; //  returns "undefined"
    }   else if (typeof value === "function"){  //  tests if value is a function
        return "function";  //  returns "function"
    }   else if (value === null){   //  tests if value is null
        return "null";  // returns "null"
    }   else{   //  any other condition
        if (Array.isArray(value)){  // tests if value is an array
            return "array"; //  returns array
        }   else {  //  any other object
            return "object";    //  returns "object"
        }
    }
}
 
_.first = function(arr, num){
    if (!Array.isArray(arr) || num < 0){   //  tests if arr is not an array
        return [];  //  returns an empty array
    }   else{   //  else, arr is an array
        if (num === undefined){ //  tests if num is not passed
            return arr[0];  //  returns first element of array
        }   else {  //  else, array is array and num is passed
            return arr.slice(0, num);//  return the first num elements of the array
        }
    }
}
 
_.last = function(arr, num){
    if (!Array.isArray(arr) || num < 0){    //   tests if arr is an array OR num is negative
        return [];  //  returns empty array
    }   else {  //  else, arr is an array
        if (num === undefined){ //  tests if num is passed
            return arr[arr.length - 1]; //  if true, returns the last element of the array
        }   else if (num > arr.length){ //  tests if num is greater than the length of array
            return arr; //  returns whole arr
        }   else {  //  else, num is passed
            return arr.slice(arr.length - num);  //  returns the last num elements of the array
        }
    }
}
 
_.indexOf = function(arr, val){
    for (var i = 0; i < arr.length; i ++){//  iterate through arr
        if (arr[i] === val){//  test if current element is val
            return i;//  if true, return element index
        }
    }
 return -1;   //  return -1 in outermost code block
}
 
_.contains = function(array, value){
    //  tests if array includes value.  Returns true if true, false if false
    return (array.includes(value)? true : false);
}
 
_.each = function(collection, func){
    if (Array.isArray(collection)){ //  tests if collection is an array
        for (var i = 0; i < collection.length; i++){    //  iterate through array
            func(collection[i], i, collection)  //  call function for each element, index, and collection
        }
    }   else {  //  else it's an object
        for (var key in collection){    //  iterate through object
            func(collection[key], key, collection)  //  call function for each property value, key, collection
        }
    }
}
 
_.unique = function(array){
    let newArr = [];    //  initialize newArr array
    for (var i = 0; i < array.length; i++){ //  iterate through array
        if (newArr.indexOf(array[i]) === - 1){  //  tests if newArr does not contain current iteration element
            newArr.push(array[i]);  //  push current element into newArr
        }
    }
return newArr;  //  returns newArr
}
 
_.filter = function(array, func){
    var newArray = [];  //  initialize newArray with value of empty of array
    for (var i = 0; i < array.length; i++){ //  iterate over array
        if (func(array[i], i, array)){  //  test if passing element, index, and array returns true
            newArray.push(array[i]);    //  if true, push element into newArray
        }
    }
return newArray;    //  returns newArray
}
 
_.reject = function(array, func){
    var newArray = [];  //  initialize newArray
    for (var i = 0; i < array.length; i++){ //  iterate over array
        if (!func(array[i], i, array)){ //  test if passing element, index, and array returns not true
            newArray.push(array[i]);    //  if not true, push into newArray
        }
    }
return newArray;    //  returns newArray
}
 
_.partition = function(array, func){
    var newArray = [[],[]]; //  initialize newArray with two sub arrays
    for (var i = 0; i < array.length; i++){ //  iterate through array
        if (func(array[i], i, array)){  //  test if passing element, index, or array is truthy
            newArray[0].push(array[i]); //  if truthy, push element into newArray[0]
        }   else {  //  else, is falsy
            newArray[1].push(array[i]); //  pushes element into newArray[1]
        }  
    }
return newArray;    //  returns newArray
}
 
_.map = function(collection, func){
var newArray = [];  //  initialize newArray
    if (Array.isArray(collection)){ //  test if collection is an array
        for (var i = 0; i < collection.length; i++){    //  if true, iterate through array
            newArray.push(func(collection[i], i, collection));  //  push the result of passing element, index, or collection into newArray
        }    
    }   else {  //  else, collection is an object
        for (var key in collection){    //  iterate through collection object
            newArray.push(func(collection[key], key, collection));  //  push the result of passing value, property or collection into func
        }
    }
return newArray;    //  returns newArray
}
 
_.pluck = function(array, property){
    //  return f
   return array.map(function(value){
    return value[property];
    });
}
 
_.every = function(collection, func){
    //  determine if collection is array
    if (Array.isArray(collection)){
        //determine if func wasn't passed in
        if (func === undefined){
            for (var i = 0; i < collection.length; i++){  //   iterate
            //determine if the current item is not truthy (faster)
            if (!collection[i]){
                // return false
                return false;
                }
            }
        }else {//  is array and func was passed
            for (let i = 0; i < collection.length; i++){
                //determine if current value return false when passed into func
                if (func(collection[i], i, collection) === false){
                    return false;
                }
            }
        }
    }else { // it was an object
        if (func === undefined){//  determine if current value returns false when passed into func
            for (var key in collection){//  
                if (!collection[key]){
                    return false;
                    }
                }
            }else { //  func is passed
                for (var key in collection){    //  iterate through object
                    if (func(collection[key], key, collection) === false){
                        return false;
                    }
                }
            }
    }  
    return true;
}
 
_.some = function(collection, func){
    if (Array.isArray(collection)){ //  test if collection is array
            if (func === undefined){ // test if func is not passed
                for (var i = 0; i < collection.length; i++){    //  loop through collection
                    if (collection[i]){ //  test if at least one item is true
                        return true;    //  return true
                    }
                }
            }else{  //  func is passed
                for (var i = 0; i < collection.length; i++){    //  iterate through collection
                    if (func(collection[i], i, collection) === true){    // Test if element is true
                        return true;    // return true
                    }
                }
            }
    }else { //  else collection is an object
        if (func === undefined){ //  test if func is not passed
            for (var key in collection){    //  iterate through object
                if (func(collection[key], key, collection)){    //  Test if element is true
                    return true;    //  return true
                }
            }
        }else {
            for (var key in collection){
                if (func(collection[key], key, collection)){
                    return true;
                }
            }
        }
    }
    return false;
}
 
*   _.reduce([1,2,3], function(previousSum, currentValue, currentIndex){ return previousSum + currentValue }, 0) -> 6
*/
 
//seed value initializes return value
//reult = 0
//iterate through array
    //  0
        //retult = func(0, 1, 0, [...])
_.reduce = function(array, func, seed){
    //  create result variable
    let result;
    //  determine if seed did not receive a value
    if (seed === undefined){
        result = array[0];
        for (let i = 1; i < array.length; i++){ //  iterate through array at 1 index
            result = func(result, array[i], i, array);  //
        }
    } else {    //  else it did
        result = seed;
        for (let i = 0; i < array.length; i++){
            result = func(result, array[i], i, array);
        }
    }
 
return result;
}
 
_.extend = function(object1, ...moreObjects){
    for (var i = 0; i < moreObjects.length; i++){//  iterate through moreObjects
        Object.assign(object1, moreObjects[i]);// assign current iteration to object1
    }
    return object1;//  return object
}

saving score / loading statistics ...