The properties are set correctly, but the syntax you provided for repeat() is incorrect. The problem is that you forgot a comma. Here are corrected lines of code:
grid.style.gridTemplateColumns = `repeat(${x}, 4fr 1fr 4fr 1fr 4fr)`;
grid.style.gridTemplateRows = `repeat(${y}, 4fr 1fr 4fr 1fr 4fr)`;