How can i calculate changes in stats for a RPG game?

I am trying to make a little text-based rpg to refine my js skills and Im running into an issue calculating things such as:
when you buy hp potion, i want to check whether character needs hp, has enough gold, and caps hp gain at maxHp variable
i want to add the value to my displayed hp variable but i want to cap it at a max value.
tried posting code but im on my phone and would take an hour to format.

ive tried using IF statements but ran into issues and i wouldnt work, also tried to use for loops but i dnt get how to make tht work. how i run a for loop that caps at a certain int and starts incrimenting at a certain point?