Базово так:
-let randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);
-button1.style.backgroundColor = randomColor;
-button2.style.backgroundColor = randomColor;
-button3.style.backgroundColor = randomColor;
-button4.style.backgroundColor = randomColor;
+let randomColor = () => '#' + Math.floor(Math.random()*16777215).toString(16);
+button1.style.backgroundColor = randomColor();
+button2.style.backgroundColor = randomColor();
+button3.style.backgroundColor = randomColor();
+button4.style.backgroundColor = randomColor();
Но лучше так:
const buttons = document.querySelectorAll('[id^="color"]');
const randomNumber = (max) => Math.round(Math.random() * max);
const generateColor = () => `hsl(${randomNumber(360)}, ${randomNumber(100)}%, ${randomNumber(100)}%)`;
for (const button of buttons) {
button.style.setProperty('background-color', generateColor());
}