The number of valenc electrons found in a representative element is determined by which group it is in in the Periodic Table.
Hydrogen is in Group IA so it has 1 valence electron
Carbon is in Group IVA so it has 4 valence electrons
Nitrogen is in group VA so it has 5 valence electrons
Oxygen is in Group VIA so it has 6 valence electrons
Chlorine is in Group VIIA so it has 7 valence electrons.
So the molecule NH3 has 8 valence elctrons total (five from nitrogen and one each from three hydrogens)
HCl has 8 valence electrons
CO2 has 16 valence electrons
CH3COOH has 24 valence electrons (8 from carbons, 12 from oxygens, 4 from hydrogen)