Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript problem with duplicate images js html

lap

New Coder
i have this script that is showing my images randomly but i dont know how to stop the images being duplicates , im very new to js and would greatly apreciate any help thank you 🙂


HTML:
<table cellpadding="0" cellspacing="0" width="100%">
   <tr>
    <td><img src "" class="img"></td>
    <td><img src "" class="img"></td>
    <td><img src "" class="img"></td>
  </tr>
 <tr>
    <td><img src "" class="img"></td>
    <td><img src "" class="img"></td>
    <td><img src "" class="img"></td>
  </tr>
  <tr>
    <td><img src "" class="img"></td>
    <td><img src "" class="img"></td>
    <td><img src "" class="img"></td>
  </tr>
           
</table>
           
             <script>
      window.onload = function() {
  let myPics = ['images/img correct/1.jpg',
                'images/img correct/2.jpg',
                'images/img correct/3.jpg',
                'images/img correct/4.jpg',
                'images/img correct/5.jpg',
                'images/img correct/6.jpg',
                'images/img correct/7.jpg',
                'images/img correct/8.jpg',
                'images/img correct/9.jpg',
                'images/img correct/10.jpg',
                'images/img correct/11.jpg',
                'images/img correct/12.jpg',
                'images/img correct/13.jpg',
                'images/img correct/14.jpg',
                'images/img correct/15.jpg',
                'images/img correct/16.jpg',
                'images/img correct/17.jpg',
                'images/img correct/18.jpg',
  ];

  function showPics() {
    document.querySelectorAll('.img').forEach(function(tag) {
      let index = getRandomIndex();
      tag.src = myPics[index];
      tag.addEventListener('click', getImageName);
      tag.dataset.index = index;
    })
  }

  function getRandomIndex() {
    return Math.floor(Math.random() * myPics.length);
  }

  function getImageName() {
     alert(myPics[this.dataset.index])
  }

  showPics();
}
    </script>
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom