0

Thank you for reading my question. I am trying to choose 5 random items from an array and insert those into 5 divs in a html document. What is the best way to ensure no duplicates? (as you can see my if statement is looking a bit shabby)

      function randomKSC1() {
       return job1[Math.floor(Math.random() * job1.length)];
       }

     $(document).ready(function() { 

        var jobnumber = (sessionStorage.getItem("jobID"));
        var ksc1 = document.getElementById("title1");
        var ksc2 = document.getElementById("title2");
        var ksc3 = document.getElementById("title3");
        var ksc4 = document.getElementById("title4");
        var ksc5 = document.getElementById("title5");

        if (jobnumber == 1) {
        $("h1#jobtitle").html("Sensory Analysis");
        $(ksc1).html(randomKSC1);
        $(ksc2).html(randomKSC1);
            while ((ksc2) == (ksc1)) {
                $(ksc2).html(randomKSC1);
        }
        $(ksc3).html(randomKSC1);
            while ((ksc3) == (ksc2) || (ksc3) ==  (ksc1)) {
                $(ksc3).html(randomKSC1);
        }
        $(ksc4).html(randomKSC1);
            while ((ksc4) == (ksc3) || (ksc4) == (ksc2) || (ksc4) == (ksc1)) {
                $(ksc4).html(randomKSC1);
        }
        $(ksc5).html(randomKSC1);
            while ((ksc5) == (ksc4) || (ksc5) == (ksc3) || (ksc5) == (ksc2) ||(ksc5) == (ksc1)) {
                $(ksc5).html(randomKSC1);
        }

    }

0 Answers0