I'm working on a web site that shows random articles in the footer. Random articles should be 4 and these articles should not be duplicated. How to make a loop through arrays to make it work.
Here is the code and fiddle for one article I made. Thanks!
FIDDLE: https://jsfiddle.net/3db8Leor/1/
HTML:
<section class="cities">
<a class="city" target="_blank">
<img src="" width="200">
<h2></h2>
</a>
<!--
<a class="city" target="_blank">
<img src="" width="200">
<h2></h2>
</a>
<a class="city" target="_blank">
<img src="" width="200">
<h2></h2>
</a>
<a class="city" target="_blank">
<img src="" width="200">
<h2></h2>
</a>
-->
</section>
JS:
const link = [
'https://www.croatiaweek.com',
'https://www.pinebeach.hr/',
'https://www.dnevnik.hr/',
'https://www.costacruises.com/',
'https://www.zadar.hr/',
'https://www.croatia.hr/'
];
const title = [
'Zagreb - Croatia',
'Pakostane - Croatia',
'Hvar - Croatia',
'Dubrovnik - Croatia',
'Zadar - Croatia',
'Brac - Croatia'
];
const image = [
'https://www.croatiaweek.com/wp-content/uploads/2015/02/KingTomislav.jpg',
'https://www.pinebeach.hr/photos/modul_2/18052017224635_pine-beach-pakostane-0020.jpg',
'https://image.dnevnik.hr/media/images/920x695/Jul2019/61719533.jpg',
'https://www.costacruises.com/content/dam/costa/inventory-assets/ports/DBV/24-DUBROVNIK_2880x1536.jpg.image.750.563.low.jpg',
'https://www.hdz-zadar.hr/public/img/home/3_mobile.png',
'https://s27135.pcdn.co/wp-content/uploads/2019/06/Brac-island-878x585.jpg.optimal.jpg'
];
const random = Math.floor(Math.random() * title.length);
const city = document.querySelector('.city');
city.href = link[random];
city.querySelector('h2').innerHTML = title[random];
city.querySelector('img').src = image[random];