Unique Characters In A String Challenge

I found a challenge on Codecademy about building a function that checks a string to see if all of the characters are unique. It seemed like a pretty straightforward challenge. Here is what I came up with using javascript:

function dupeCheck() {
  var x = 'qsxthfeavyjkowmp'
  var dupe = 0;
  for (i = 0; i < x.length; i++) {
   var letter = x.charAt(i);
   var checker = x.indexOf(letter, i+1);
   if (checker > 0) {dupe = dupe + 1;}
  }
  if (dupe > 0) {
   alert("Duplicate Characters Were Found In Your String")
  } else {
   alert("Your string does not contain duplicate characters.")
  }
 }

Originally, I was passing the string as a variable, but to turn it in, I switched it to a variable in the function. I think it’s pretty self explanatory. I’m taking each character in the string and checking to see if it appears again in the string. If it does, I add to the counter.

Leave a Reply

Your email address will not be published. Required fields are marked *