Unique Characters in a String Challenge

Hello!

I have tried this challenge: Unique Characters in a String

…Adn have found this working solution:

function uniqueCharacters(stringIn) {
  var stringLength = stringIn.length;

  if(stringIn==""){return 'Error'}

  for (let i = 0; i < stringLength; i++) {
    var a = stringIn.charAt(i);
    var b = stringIn.replace(a, '');
    if(b.search(a)!=-1){return false}
    }

  return true;

}

uniqueCharacters('aab');

Now my question is: how can I optimize this function? What would be a better way to write this with fewer lines?

Thanks in advance for your help!

function uniqueCharacters(stringIn) { return stringIn.length < 1 ? "Error!" : stringIn.length === [...new Set(stringIn)].length } console.log(uniqueCharacters('abcdef')) console.log(uniqueCharacters('abccdef')) // Leave this so we can test your code: module.exports = uniqueCharacters;

My understanding is that String.search() takes a regular expression, not a string. Have you passed the challenge test with this code?

On good advice, we should be true to form, rather then bending things to our wishes…

function uniqueCharacters(stringIn) { return stringIn.length < 1 ? "Error!" : stringIn.length === new Set(stringIn).size; } console.log(uniqueCharacters('abcdef')) console.log(uniqueCharacters('abccdef'))

Hello! Thanks for your reponse. Yes I have passed the test with this solution. I wanted to see how to make it better…

Thanks again! I will look at your solution today! I have never seen the “new Set” before…