title: Javascript | Dynamic array of Characters based on consecutive letters conditions
body: <p>I was doing a codewar challange, and couldn't find a solution, but I really want to know how we can solve this problem.</p>
  
  <p>So we getting two integers, let's say N and D and we should return a string containing exactly N letters 'n' and exactlly D letters d with no three consecutive letters being same. </p>
  
  <p>For example if we get N=5 and D=3 we should return "nndnndnd" or "nbnnbbnn" or any other correct answer</p>
  
  <p>another example like if we get N=1 D=4 the only accepted answer should be "ddndd"</p>
  
  <p>What I did was making a helper function like this : </p>
  
  <pre><code>function generateArray (char,q){
  let arr= []
  for(let i=0; i&lt;q; i++){
      arr.push(char)
  }
  
  return arr
  }
  </code></pre>
  
  <p>and inside the main function : </p>
  
  <pre><code>function solution(N, D) {
  let arrayOfchar = generateArray('n',N)
  
      arrayOfchar.reduce((prev,current,index) =&gt; {
          for(let i=0; i&lt;D; i++) {
          if(prev===current) {
              arrayOfchar.splice(index, 0, "d")
          }
      }
      })
  }
  </code></pre>
  
  <p>But I don't know hoe should I put the "d" only after two or less consecutive "n"</p>
  
  <p>Anyone clue?</p>