Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Avoid duplication image JS

How to avoid duplication photo while displaying random photos each time?

let photosList = [
    './images/1.jpg',
    './images/2.jpg',
    './images/3.jpg',
    './images/4.jpg',
    './images/5.jpg',
    './images/6.jpg'
];

function RandomIndex() {
    return Math.floor(Math.random() * photosList.length);
}

function showList() {
    document.querySelectorAll('.classImg').forEach(function(tag) {
        let index = RandomIndex();
        tag.src = photosList[index];
    })
}

showList();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do something like this:

let photosList = [
    './images/1.jpg',
    './images/2.jpg',
    './images/3.jpg',
    './images/4.jpg',
    './images/5.jpg',
    './images/6.jpg'
];

let photoSelected = new Array(photosList.length).fill(false);

function RandomIndex() {
    return Math.floor(Math.random() * photosList.length);
}

function showList() {
    document.querySelectorAll('.classImg').forEach(function(tag) {
        let index = RandomIndex();
        while(photoSelected[index] == true){
            index = RandomIndex();
        };
        photoSelected[index] = true;
        tag.src = photosList[index];
    })
}

showList();

Hope, it helps!!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!