Dan had $ w left with him after he bought a candy bar. If he had $ x at the start How much did the candy bar cost?
Dan had w left with him after he bought a candy bar. 

This means that at some point, he had w + x. 

Then he bought a candy bar, which means that he had to spend some money on the candy bar. 

Therefore, the candy bar cost him (w + x) - w, or x.