From 42237471ab4a6c9d6b63a6fbf7f87072e2ad91c0 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Wed, 13 Dec 2017 21:42:58 +0530 Subject: [PATCH 1/6] Create join_array_like_objects.md --- snippets/join_array_like_objects.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 snippets/join_array_like_objects.md diff --git a/snippets/join_array_like_objects.md b/snippets/join_array_like_objects.md new file mode 100644 index 000000000..57acd6731 --- /dev/null +++ b/snippets/join_array_like_objects.md @@ -0,0 +1,11 @@ +### Joining an array-like object + +The following example joins array-like object (arguments), by calling Function.prototype.call on Array.prototype.join. + +``` +function f(a, b, c) { + var s = Array.prototype.join.call(arguments); + console.log(s); // '1,a,true' +} +f(1, 'a', true); +``` From 8f839542b61a6bb0188146c3f06be68a169c742d Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Wed, 13 Dec 2017 21:52:51 +0530 Subject: [PATCH 2/6] Create check_for_boolean --- snippets/check_for_boolean | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 snippets/check_for_boolean diff --git a/snippets/check_for_boolean b/snippets/check_for_boolean new file mode 100644 index 000000000..a96719aea --- /dev/null +++ b/snippets/check_for_boolean @@ -0,0 +1,10 @@ +### Check for Boolean Primitive Values + +Check if a value is classified as a boolean primitive. Return true or false. + +function booWho(bool) { + return typeof bool === 'boolean'; +} + +// test here +booWho(null); From e82141f80155f30f0a503a0d6e66db05cee9d241 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Wed, 13 Dec 2017 21:56:13 +0530 Subject: [PATCH 3/6] Rename check_for_boolean to check_for_boolean.md --- snippets/{check_for_boolean => check_for_boolean.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{check_for_boolean => check_for_boolean.md} (100%) diff --git a/snippets/check_for_boolean b/snippets/check_for_boolean.md similarity index 100% rename from snippets/check_for_boolean rename to snippets/check_for_boolean.md From 32c27187a450d240b39cffb3af2b782431d47165 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Wed, 13 Dec 2017 22:00:06 +0530 Subject: [PATCH 4/6] Create drop_elements_in_array.md --- snippets/drop_elements_in_array.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/drop_elements_in_array.md diff --git a/snippets/drop_elements_in_array.md b/snippets/drop_elements_in_array.md new file mode 100644 index 000000000..73e2688b5 --- /dev/null +++ b/snippets/drop_elements_in_array.md @@ -0,0 +1,19 @@ +### Drop It + +Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true. + +Method - +- Use a while loop with Array.prototype.shift() to continue checking and dropping the first element of the array until the function returns true. It also makes sure the array is not empty first to avoid infinite loops. +- Return the filtered array. + +``` +function dropElements(arr, func) { + while(arr.length > 0 && !func(arr[0])) { + arr.shift(); + } + return arr; +} + +// test here +dropElements([1, 2, 3, 4], function(n) {return n >= 3;}); +``` From d9662f3953889b072c01492b984d337c7cfc9733 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:32:08 +0200 Subject: [PATCH 5/6] Update and rename check_for_boolean.md to check-for-boolean-primitive-values.md --- snippets/check-for-boolean-primitive-values.md | 8 ++++++++ snippets/check_for_boolean.md | 10 ---------- 2 files changed, 8 insertions(+), 10 deletions(-) create mode 100644 snippets/check-for-boolean-primitive-values.md delete mode 100644 snippets/check_for_boolean.md diff --git a/snippets/check-for-boolean-primitive-values.md b/snippets/check-for-boolean-primitive-values.md new file mode 100644 index 000000000..308e3a085 --- /dev/null +++ b/snippets/check-for-boolean-primitive-values.md @@ -0,0 +1,8 @@ +### Check for boolean primitive values + +Use `typeof` to check if a value is classified as a boolean primitive. + +```js +const isBool = val => typeof val === 'boolean'; +// isBool(null) -> false +``` diff --git a/snippets/check_for_boolean.md b/snippets/check_for_boolean.md deleted file mode 100644 index a96719aea..000000000 --- a/snippets/check_for_boolean.md +++ /dev/null @@ -1,10 +0,0 @@ -### Check for Boolean Primitive Values - -Check if a value is classified as a boolean primitive. Return true or false. - -function booWho(bool) { - return typeof bool === 'boolean'; -} - -// test here -booWho(null); From ce2cd00079868c5b27d3b47e2bc820c87194b12f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 11:36:20 +0200 Subject: [PATCH 6/6] Update and rename drop_elements_in_array.md to drop-elements-in-array.md --- snippets/drop-elements-in-array.md | 12 ++++++++++++ snippets/drop_elements_in_array.md | 19 ------------------- 2 files changed, 12 insertions(+), 19 deletions(-) create mode 100644 snippets/drop-elements-in-array.md delete mode 100644 snippets/drop_elements_in_array.md diff --git a/snippets/drop-elements-in-array.md b/snippets/drop-elements-in-array.md new file mode 100644 index 000000000..0ad2ab5e9 --- /dev/null +++ b/snippets/drop-elements-in-array.md @@ -0,0 +1,12 @@ +### Drop elements in array + +Loop through the array, using `Array.shift()` to drop the first element of the array until the returned value from the function is `true`. +Returns the remaining elements. + +```js +const dropElements = (arr,func) => { + while(arr.length > 0 && !func(arr[0])) arr.shift(); + return arr; +} +// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4] +``` diff --git a/snippets/drop_elements_in_array.md b/snippets/drop_elements_in_array.md deleted file mode 100644 index 73e2688b5..000000000 --- a/snippets/drop_elements_in_array.md +++ /dev/null @@ -1,19 +0,0 @@ -### Drop It - -Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true. - -Method - -- Use a while loop with Array.prototype.shift() to continue checking and dropping the first element of the array until the function returns true. It also makes sure the array is not empty first to avoid infinite loops. -- Return the filtered array. - -``` -function dropElements(arr, func) { - while(arr.length > 0 && !func(arr[0])) { - arr.shift(); - } - return arr; -} - -// test here -dropElements([1, 2, 3, 4], function(n) {return n >= 3;}); -```