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 '
Array.reduce() to create an object, where the keys are produced
const initial = arr => arr.slice(0, -1);
// initial([1,2,3]) -> [1,2]
+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]]
+
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]]
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')
+
+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
+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
+
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.
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
join('').
str.split('').sort((a, b) => a.localeCompare(b)).join('');
// sortCharactersInString('cabbage') -> 'aabbceg'
+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"]
+
Converts a string to camelcase.
Use replace() to remove underscores, hyphens and spaces and convert words to camelcase.