The test:

   if (p[g])

(I think) is testing for the existence of a parameter value. But if the value is 0 the test fails. So the test should be:
  
   if (p[g] || p[g] === 0)