Ratios can be scaled up or down by multiplication or division.
So for example the ratio 4 : 7 is the same as 8 : 14 (having multiplied by 2), or 12 : 21 ( having multiplied by 3) and so on.
With problems like this, where two ratios are involved, the thing to do is to arrange for two of the components, (in different ratios) to be equal to each other. So, multiply the first ratio by 9 and the second by 7 to arrive at
J : P = 36 : 63, and P : S = 63 : 35.
The two can then be shunted together so J : P : S = 36 : 63 : 35.
The actual number of marbles for each person will then be (for some positive integer k), 36k for John, 63k for Peter and 35k for Sam.
John then gives 3k and Peter gives 7k to Sam.
That means that Sam now has 35k + 3k + 7k = 45k marbles and that has to equal 135, so k = 3, etc. .