jeudi 1 septembre 2016

Randomly generate name from Array with zero repeat?

Update: The difference is I'm not trying to make one list I'm trying to make a button that can be clicked and generate a random name

Goal: Click a button and randomly generate a name from an array. I'm trying to be able to click the button and show one name randomly at a time with no repeating names. So far I've been able to randomly select a name but the names still repeat. How could I change my code to avoid any repeating names?

 $( ".next").click(function() {
     $(".intro").hide()
     var people = ["Andrew", "Adam", "Seth", "Mattos", "Eric"];
    for(i=0;i<1;i++){
    var randomPosition = Math.floor(Math.random() * people.length);
    var selected = people.splice(randomPosition,1);
    console.log(selected)

    $('#person').text(selected)


    if ($('#person').text() === "Mattos"){
        $("#selectedPerson").text("Mattos")
    }
    if ($('#person').text() === "Andrew"){
        $("#selectedPerson").text("Andrew")
    }
    if ($('#person').text() === "Eric"){
        $("#selectedPerson").text("Eric")
    }

    if ($('#person').text() === "Seth"){
        $("#selectedPerson").text("Seth")
    }
    if ($('#person').text() === "Adam"){
        $("#selectedPerson").text("Adam")
    }
    }
    });




Aucun commentaire:

Enregistrer un commentaire