Code Challenge: Rename filenames if duplicates are present
<p>I'm working through a coding challenge I found online.  I have the first test case passing but failing the second.  I'm trying to decide if the second test case I'm failing is a typo or not.</p>

<p>Here is the question:</p>

<blockquote>
  <p>You are given an array of desired filenames in the order of their
  creation. Since two files cannot have equal names, the one which comes
  later will have an addition to its name in a form of (k), where k is
  the smallest positive integer such that the obtained name is not used
  yet.</p>
  
  <p>Return an array of names that will be given to the files.</p>
</blockquote>

<p>Test Cases:</p>

<p>1 - Passing: </p>

<ul>
<li>INPUT: <code>["doc", "doc", "image", "doc(1)", "doc"]</code></li>
<li>OUTPUT: <code>["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"]</code></li>
</ul>

<p>2 - Failing:</p>

<ul>
<li>INPUT: <code>["a(1)","a(6)","a","a","a","a","a","a","a","a","a","a"]</code></li>
<li>OUTPUT: <code>["a(1)","a(6)","a","a(2)","a(3)","a(4)","a(5)","a(7)","a(8)","a(9)","a(10)","a(11)"]</code></li>
</ul>

<p>Here is my code that passes the first Spec:</p>

<pre><code>function fileNaming(names) {
    var finalArr = [],
        obj = {};
    names.forEach(function(val){

        if(obj[val] === undefined){
            if(finalArr.indexOf(val) === -1){
              finalArr.push(val);  
              obj[val] = 0;
            } else {
              obj[val] = 1;
              finalArr.push(val + "(" + obj[val] + ")" );
            }

        } else {
            finalArr.push( val + "(" + (++obj[val]) + ")");
        }
    }); 
    return finalArr;
}
</code></pre>

<p>Question:</p>

<ul>
<li>In the second test spec, why isn't there an <code>"a(1)(1)"</code> like there is a <code>"doc(1)(1)"</code> Is this a typo?</li>
<li>If anyone has suggestions on improvining my approach or alternative approaches I would greatly appreciate your feedback.</li>
</ul>