In short, according to your logic, you do not need a cycle, and integer division is enough:
Math.floor rounds the result to the nearest smaller integer.
function replaceNum() { count4 = Math.floor(count2/3); count2 += 1; }
This code has a number of other stylistic problems, but we'll probably talk about this in the next series)
One of these problems is the naming of variables.
It is not necessary to name the variables count2, count4. Try to call them as meaningfully as possible, it will help when you understand this.
The name of a variable usually consists of one or more English words written in a row. To separate one word from another, each of the following is usually called with a capital letter. This style is called camelCase (camel notation) For example
myAwesomeVariable = 15;
Actually, if you change the names of the variables in your code to meaningful ones, it will be like this. Of course, this is ambiguous, you can come up with your beautiful names (in js, by the way, variables in Russian can be called but never, never, never show them to anyone =)); In any case, it will be better than count2, count4
function processCardIndexes() { cardStackIndex = Math.floor(cardTotalIndex/3); cardTotalIndex += 1; }