dev-resources.site
for different kinds of informations.
JS Challenge: Check if all elements in an array are the same
Published at
8/15/2023
Categories
javascript
javascripttips
jschallenge
oneliners
Author
Justin
One solution is to verify each element in the array is equal to the first element of the array.
Alternately we can also filter which are not equal to the first element and verify the resulting array length is zero.
arr1 = [1, 1, 1, 1, 1];
arr2 = [1, 1, 0, 1, 1];
arr1.every((e,i,a) => e === a[0]); //returns true
arr2.every((e,i,a) => e === a[0]); //returns false
//OR
arr1.filter((e,i,a) => e !== a[0]).length===0; //returns true
arr2.filter((e,i,a) => e !== a[0]).length===0; //returns false
Articles
12 articles in total
JS Challenge: Check if all elements in an array are the same
currently reading
JS Challenge: Sum of the values of an object's properties
read article
JS Challenge: Check if an array has all its values sorted in the ascending order
read article
JS Challenge: Count the occurrences of a character/pattern in a string
read article
JS Challenge: Reverse the characters of each word in a string
read article
JS Challenge: Check if a string is a palindrome
read article
JS Challenge: Create an array and fill with serial numbers
read article
JS Challenge: Remove duplicates from an array
read article
JS Challenge: Fold a long line of text into lines of 80ish characters without breaking words
read article
JS Challenge: Convert snake_case to camelCase
read article
Duplicating Line(s) in Chrome DevTools Editor
read article
How To Quickly Generate Google Fonts Preview
read article
Featured ones: