Last active
May 30, 2022 09:13
-
-
Save BenDMyers/2f75e75522b5c0c11d1e06d8c6b9506a to your computer and use it in GitHub Desktop.
RWC: All Unique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Determines whether all characters in a string are unique. | |
* | |
* Original question: | |
* **Write a function that determines if all the characters in a given string are unique.** | |
* Can you do this without making any new variables? You choose if you want to include | |
* capitalization in your consideration for this one, as a fun challenge. | |
* | |
* @param {string} str string to test | |
* @param {boolean} [caseSensitive] whether to take capitalization into account | |
* @returns {boolean} whether all characters in the string are unique | |
*/ | |
function allUnique(str, caseSensitive = true) { | |
return caseSensitive ? | |
new Set(str.split('')).size === str.length : | |
new Set(str.toLowerCase().split('')).size === str.length; | |
} | |
console.log(allUnique('Cassidy')); // false | |
console.log(allUnique('cat & dog')); // false | |
console.log(allUnique('cat+dog')); // true | |
console.log(allUnique('Cc')); // true | |
console.log(allUnique('Cc', false)); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice 🙂