0

INSTRUCTIONS

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

EXAMPLE

  • "abcde" -> 0 # no characters repeats more than once
  • "aabbcde" -> 2 # 'a' and 'b'
  • "aabBcde" -> 2 # 'a' occurs twice and 'b' twice (b and B)
  • "indivisibility" -> 1 # 'i' occurs six times
  • "Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
  • "aA11" -> 2 # 'a' and '1'
  • "ABBA" -> 2 # 'A' and 'B' each occur twice

ATTEMPT

function duplicateCount(text){
  text = text.toUpperCase();

  let count = 0;

  for(let i of text){
    if(text[i] === text[++i]){
      count++
    }
  }
  return count;
}

TEST CASE

  • Test.assertEquals(duplicateCount(""), 0);
  • Test.assertEquals(duplicateCount("abcde"), 0);
  • Test.assertEquals(duplicateCount("aabbcde"), 2);
  • Test.assertEquals(duplicateCount("aabBcde"), 2,"should ignore case");
  • Test.assertEquals(duplicateCount("Indivisibility"), 1)
  • Test.assertEquals(duplicateCount("Indivisibilities"), 2, "characters may not be adjacent")

TEST RESULTS

  • Test Passed: Value == 0
  • Expected: 0, instead got: 5
  • Expected: 2, instead got: 7
  • should ignore case - Expected: 2, instead got: 7
  • Expected: 1, instead got: 14
  • characters may not be adjacent - Expected: 2, instead got: 16

What am I missing here? I'm only passing the first test case.

and1
  • 307
  • 2
  • 10
  • Also https://stackoverflow.com/questions/28807016/get-duplicate-characters-count-in-a-string and https://stackoverflow.com/questions/18619785/counting-frequency-of-characters-in-a-string-using-javascript – Nick Mar 29 '20 at 02:53
  • 1
    You have `let i of text` so `i` is each character. There is no need to do `text[i]` to get those characters. Since `text[i]` is undefined, all the if statements are true so it effectively just counts the characters in the string. – takendarkk Mar 29 '20 at 02:54
  • Appreciate the links but still not sure how to fix my code – and1 Mar 29 '20 at 02:56
  • @takendarkk gotcha, so change it to `if(i === ++i)`? I just tried that and now the 2nd test case passes but that's it – and1 Mar 29 '20 at 02:57
  • Because you can't `++` a character like that, only numbers. Just use a regular for loop instead of `let i of text`. Also keep in mind that your code does not account for duplicate characters which are not right next to each other. – takendarkk Mar 29 '20 at 02:57
  • @takendarkk so how can i revise that part? – and1 Mar 29 '20 at 02:58

0 Answers0