function bg() {
var imgCount = 3;
var dir = 'http://local.statamic.com/_themes/img/';
// I changed your random generator
//floor: helps getting a random integer
var randomCount = (Math.floor(Math.random() * imgCount));
// I changed your array to the literal notation. The literal notation is preferred.
var images = ['001.png', '002.png', '003.png'];
// I changed this section to just define the style attribute the best way I know how.
document.getElementById('banner').setAttribute("style", "background-image: url(" + dir + images[randomCount] + ");background-repeat: no-repeat;background-size: 388px 388px");
}
// Don't forget to run the function instead of just defining it.
bg();
//Dynamically set the background image using js
let imageLink = 'https://images.pexels.com/photos/3928264/pexels-photo-3928264.jpeg?auto=compress&cs=tinysrgb&w=600&lazy=load'
document.getElementById(
'body'
).style.backgroundImage = `url(${imageLink})`;
//Note that the url is encompassed by backticks, not single quotations.
//Backticks can be gotten on keyboard
//You can build a function and make imageLink a parameter or do anything else.
//There are many thinngs