From 321d18d91c59f841de3a65601ce9a8f31a42ca9d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 20 Dec 2017 11:30:54 +0200 Subject: [PATCH] Build --- README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++- docs/index.html | 39 +++++++++++++++++++++++++++--- tag_database | 4 ++++ 3 files changed, 102 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 715b1a3c9..8602666f2 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ * [`groupBy`](#groupby) * [`head`](#head) * [`initial`](#initial) +* [`initialize2DArray`](#initialize2darray) * [`initializeArrayWithRange`](#initializearraywithrange) * [`initializeArrayWithValues`](#initializearraywithvalues) * [`intersection`](#intersection) @@ -52,6 +53,7 @@ * [`zip`](#zip) ### Browser +* [`arrayToHtmlList`](#arraytohtmllist) * [`bottomVisible`](#bottomvisible) * [`currentURL`](#currenturl) * [`elementIsVisibleInViewport`](#elementisvisibleinviewport) @@ -85,6 +87,7 @@ * [`fibonacci`](#fibonacci) * [`gcd`](#gcd) * [`hammingDistance`](#hammingdistance) +* [`isArmstrongNumber`](#isarmstrongnumber) * [`isDivisible`](#isdivisible) * [`isEven`](#iseven) * [`isPrime`](#isprime) @@ -122,6 +125,7 @@ * [`fromCamelCase`](#fromcamelcase) * [`reverseString`](#reversestring) * [`sortCharactersInString`](#sortcharactersinstring) +* [`stringToArrayOfWords`](#stringtoarrayofwords) * [`toCamelCase`](#tocamelcase) * [`truncateString`](#truncatestring) @@ -400,6 +404,19 @@ const initial = arr => arr.slice(0, -1); [⬆ back to top](#table-of-contents) +### initialize2DArray + +Initializes an 2D array of given width and height and value. + +Use `Array.map()` to generate h rows where each is a new array of size w initialize with value. If value is not provided, default to `null`. + +```js +const initialize2DArray = (w, h, val = null) => Array(h).fill().map(() => Array(w).fill(val)); +// initializeArrayWithRange(2, 2, 0) -> [[0,0], [0,0]] +``` + +[⬆ back to top](#table-of-contents) + ### initializeArrayWithRange Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive. @@ -749,6 +766,19 @@ const zip = (...arrays) => { [⬆ back to top](#table-of-contents) ## Browser +### arrayToHtmlList + +Converts the given array elements into '
  • ' tags and appends them to the list of the given id. + +Use `Array.map()` and `document.querySelector()` to create a list of html tags. + +```js +const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`
  • ${item}
  • `); +// arrayToHtmlList(['item 1', 'item 2'],'myListID') +``` + +[⬆ back to top](#table-of-contents) + ### bottomVisible Returns `true` if the bottom of the page is visible, `false` otherwise. @@ -1190,6 +1220,22 @@ const hammingDistance = (num1, num2) => [⬆ back to top](#table-of-contents) +### isArmstrongNumber + +Checks if the given number is an armstrong number or not. + +Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. + +```js +const isArmstrongNumber = digits => + ( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) ); +// isArmstrongNumber(1634) -> true +// isArmstrongNumber(371) -> true +// isArmstrongNumber(56) -> false +``` + +[⬆ back to top](#table-of-contents) + ### isDivisible Checks if the first numeric argument is divisible by the second one. @@ -1530,7 +1576,7 @@ Checks if the predicate (second argument) is truthy on all elements of a collect Use `Array.every()` to check if each passed object has the specified property and if it returns a truthy value. ```js -truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre])); +const truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre])); // truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true ``` @@ -1659,6 +1705,21 @@ const sortCharactersInString = str => [⬆ back to top](#table-of-contents) +### stringToArrayOfWords + +Converts a given string into an array of words. + +Use `String.split()` with a supplied pattern (defaults to non alpha as a regex) to convert to an array of strings. Use `Array.filter()` to remove any empty strings. +Omit the second argument to use the default regex. + +```js +const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean); +// stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] +// stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"] +``` + +[⬆ back to top](#table-of-contents) + ### toCamelCase Converts a string to camelcase. diff --git a/docs/index.html b/docs/index.html index a1672c5b2..f54dbf473 100644 --- a/docs/index.html +++ b/docs/index.html @@ -71,6 +71,7 @@ groupBy head initial +initialize2DArray initializeArrayWithRange initializeArrayWithValues intersection @@ -94,7 +95,8 @@ zip

    Browser -

    bottomVisible +arrayToHtmlList +bottomVisible currentURL elementIsVisibleInViewport getScrollPosition @@ -127,6 +129,7 @@ fibonacci gcd hammingDistance +isArmstrongNumber isDivisible isEven isPrime @@ -164,6 +167,7 @@ fromCamelCase reverseString sortCharactersInString +stringToArrayOfWords toCamelCase truncateString @@ -315,6 +319,12 @@ Use Array.reduce() to create an object, where the keys are produced
    const initial = arr => arr.slice(0, -1);
     // initial([1,2,3]) -> [1,2]
     
    +

    initialize2DArray

    +

    Initializes an 2D array of given width and height and value.

    +

    Use Array.map() to generate h rows where each is a new array of size w initialize with value. If value is not provided, default to null.

    +
    const initialize2DArray = (w, h, val = null) => Array(h).fill().map(() => Array(w).fill(val));
    +// initializeArrayWithRange(2, 2, 0) -> [[0,0], [0,0]]
    +

    initializeArrayWithRange

    Initializes an array containing the numbers in the specified range where start and end are inclusive.

    Use Array((end + 1) - start) to create an array of the desired length, Array.map() to fill with the desired values in a range. @@ -515,7 +525,13 @@ If lengths of the argument-arrays vary, undefined is used where no //zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]]


    Browser

    -

    bottomVisible

    +

    arrayToHtmlList

    +

    Converts the given array elements into '<li>' tags and appends them to the list of the given id.

    +

    Use Array.map() and document.querySelector() to create a list of html tags.

    +
    const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`<li>${item}</li>`);
    +// arrayToHtmlList(['item 1', 'item 2'],'myListID')
    +
    +

    bottomVisible

    Returns true if the bottom of the page is visible, false otherwise.

    Use scrollY, scrollHeight and clientHeight to determine if the bottom of the page is visible.

    const bottomVisible = () =>
    @@ -766,6 +782,15 @@ Count and return the number of 1s in the string, using match(
       ((num1 ^ num2).toString(2).match(/1/g) || '').length;
     // hammingDistance(2,3) -> 1
     
    +

    isArmstrongNumber

    +

    Checks if the given number is an armstrong number or not.

    +

    Convert the given number into array of digits. Use Math.pow() to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return true otherwise false.

    +
    const isArmstrongNumber = digits => 
    +  ( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) );
    +// isArmstrongNumber(1634) -> true
    +// isArmstrongNumber(371) -> true
    +// isArmstrongNumber(56) -> false
    +

    isDivisible

    Checks if the first numeric argument is divisible by the second one.

    Use the modulo operator (%) to check if the remainder is equal to 0.

    @@ -961,7 +986,7 @@ a === b -> false

    truthCheckCollection

    Checks if the predicate (second argument) is truthy on all elements of a collection (first argument).

    Use Array.every() to check if each passed object has the specified property and if it returns a truthy value.

    -
    truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre]));
    +
    const truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre]));
     // truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true
     

    String

    @@ -1031,6 +1056,14 @@ Combine characters to get a string using join('').

    str.split('').sort((a, b) => a.localeCompare(b)).join(''); // sortCharactersInString('cabbage') -> 'aabbceg'
    +

    stringToArrayOfWords

    +

    Converts a given string into an array of words.

    +

    Use String.split() with a supplied pattern (defaults to non alpha as a regex) to convert to an array of strings. Use Array.filter() to remove any empty strings. +Omit the second argument to use the default regex.

    +
    const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean);
    +// stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"]
    +// stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"]
    +

    toCamelCase

    Converts a string to camelcase.

    Use replace() to remove underscores, hyphens and spaces and convert words to camelcase.

    diff --git a/tag_database b/tag_database index 5eb4c145c..3a78d941c 100644 --- a/tag_database +++ b/tag_database @@ -3,6 +3,7 @@ arrayAverage:math arrayMax:array arrayMin:array arraySum:math +arrayToHtmlList:browser bottomVisible:browser capitalize:string capitalizeEveryWord:string @@ -47,9 +48,11 @@ hammingDistance:math head:array hexToRGB:utility initial:array +initialize2DArray:array initializeArrayWithRange:array initializeArrayWithValues:array intersection:array +isArmstrongNumber:math isArray:utility isBoolean:utility isDivisible:math @@ -96,6 +99,7 @@ sleep:function sortCharactersInString:string speechSynthesis:media standardDeviation:math +stringToArrayOfWords:string symmetricDifference:array tail:array take:array