Codewars "Pair of gloves" problem

Hope it’s fine to ask for some help on challenges from other sites here.
The task:

  **Your code so far**

function numberOfPairs(gloves){
 let object = {};
 for(let i = 0; i<gloves.length; i++){
 object[gloves[i]] ? object[gloves[i]]++ : object[gloves[i]] = 1
  let pairs = 0
  for(key in object){
  if (object[key] % 2 === 0) pairs += (object[key] / 2)
return pairs;

My code seems to pass all the tests however when I click attempt it fails one? I can’t even view the tests so I can’t tell where I’ve made a mistake. Is there a fault in my code or does it just work for special instances I can’t think of?

// I can’t even view the tests so I can’t tell where I’ve made a mistake.

Yes, that is by design. There are things we call “edge cases”, areas on the “edge” of what is “normal” where algorithms may fail. Part of being a developer is thinking about these things and accounting for them. They didn’t tell you what edge case they were testing - they want you to figure it out.

Stop and think where the edge cases might be. For example, if I were doing a function do divide two numbers… What if the divisor is zero? What if the answer is too big? What if one of the numbers is too big, or is Infinity? What if one of the inputs isn’t a number? Etc. How should it handle each of these?

So, I was able to find a test that failed for your solution. Since we are told:

You are given an array containing the color of each glove.

We know that it will be an array, and since the colors will be strings, we can assume that this will be an array of strings. Let’s also assume that the color strings will be lowercase and there won’t be any spelling variants. What other odd conditions might give the potential for an odd answer? Hint: It’s not something terribly sneaky.

See if you can find it. Check back if you’re still struggling.

1 Like

This was possibly the most helpful response I’ve received in the last few months. Thanks a ton I really appreciate it.

It’s happened well over a handful of times where I’ll be solving katas on this website and my code passes all the test cases and fails to solve just one or two edge cases. Typically I get incredibly frustrated, move on and bookmark the challenge to come have a think about it later. I’ve had a think about the edge cases in this specific kata and have yet to figure it out. No spoilers though, I’m gonna give it some more time.

Does figuring out edge cases eventually become second nature? Do these odd conditions repeat themselves in similar instances? I’ve been in this situation too many times.

Yeah, pretty much.
Especially the more restrictions you already start with. As kevin hinted at, we assume valid and reasonable inputs - strings, no spelling mistakes, actual colors…

I did some challengs on Leetcode and often edge cases were pretty similar - though it took looking at the input-specifications to understand and see where issues might be.
But, again as Kevin said, it’s usually nothing sneaky.


On some sites I have seen a few whee they are trying to be sneaky. I remember one on CW where the author’s solution was based on a mathematical trick that he didn’t understand was just an approximation. His solution was an approximation and all his test cases were. When I solved is accurately, it failed. It drove me crazy and took me days to figure out what was happening. I tried to point out that the solution was wrong unless the problem was clarified, I got nothing but anger.

Remember that some of these sites are just user generated content. Sometimes the problem isn’t clearly defined, and at least in a few questions, the answer is wrong. And some people do try to put in “gotcha” tests, but is the problem is well defined, those should be catchable. (But in fairness, a lot of real life problems have “gotcha” edge conditions.)

Like all things in life, be a little cautious with user generated content. I will say that I remember fewer problems with LeetCode.


Hi. I’ve looked at the problem again a couple of times and I just can’t figure it out sadly enough. What are these edge cases?

Explain to us in English exactly what this line is doing.


Wow. As I was typing up my explanation I realised that it’ll only recognise pairs of glove numbers that are even in the amount of frequency. I feel goofy :clown_face:

Thanks a ton :rofl:


Yeah, that’s one of the reasons we ask people to type up detailed explanations - sometimes it dawns on us as we’re trying to figure out how to explain it. Yeah, that would be an example of an “edge case”. Some of them can be really sneaky. It’s part of a devs job to be always thinking, “What could go wrong here?”

Good work.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.