I want to print a javascript array of images in random order but expect the middle one I want this one g.jpg to stay in it is positions right now all of them are shuffle, how to separate or absolute g.jpg position. I think I need to add a different class name for g.jpg but I don't know how to do it.
<html>
<head>
<meta charset='utf-8'>
<title></title>
<style>
.ppl{
width: 250px;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/javascript">
const images = [
'images/1.jpg',
'images/2.jpg',
'images/g.jpg',
'images/3.jpg',
'images/4.jpg'
]
const root = document.querySelector('#root')
const shuffle = ([...array]) => {
let currentIndex = array.length
let temporaryValue
let randomIndex
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
// And swap it with the current element.
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
const shuffledImages = shuffle(images)
shuffledImages.forEach(src => {
const image = document.createElement('img')
image.src = src
image.alt = src
image.classList.add('ppl')
image.classList.add('pos')
root.appendChild(image)
})
</script>
</body>
</html>