Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
Generate 4 random values from an array, but no repeating values

I have an array that holds countries. I need to figure out how to take three values from the array, and the 4th value needs to be the right answer.**

The answer choices (as radio buttons) need to be random and not repeated, but also contain the right answer and place it at a random location.

randomImage[0] = "Colombia";
randomImage[1] = "Argentina";
randomImage[2] = "Bolivia";
randomImage[3] = "Brazil";
randomImage[4] = "Paraguay";
randomImage[5] = "Venezuela";
randomImage[6] = "Ecuador";
randomImage[7] = "Peru";
randomImage[8] = "Chile";
randomImage[9] = "Uruguay";

I am using this random variable called numb to show answers randomly from the same array as below, however, the right answer does not always show up.

var number = Math.floor(Math.random() * randomImage.length);
    var numb = Math.floor(Math.random() * randomImage.length);
    if ((numb == 9) || (numb == 0)) {
        numb = 4;
    }
    else if ((numb == 8) || (numb == 1)) {
        numb = 6;
    }

This is how I am showing my answers:

function getQuestions(rand) {

    document.getElementById("questions").innerHTML = '<div><label>' + answers[rand] + '<input id= "answer1" name = "ans" type = "radio" onclick="getResults()" value = "' + answers[rand] + '"></label></div>' +
        '<div><label>' + answers[rand + 1] + '<input id= "answer2" name="ans" type="radio" onclick="getResults()" value = "' + answers[rand + 1] + '"></label></div>' +
        '<div><label>' + answers[rand - 1] + '<input id= "answer3" name="ans" type="radio" onclick="getResults()" value = "' + answers[rand - 1] + '"></label></div>' +
        '<div><label>' + answers[rand + 2] + '<input id= "answer4" name="ans" type="radio" onclick="getResults()" value = "' + answers[rand + 2] + '"></label></div>' +
        '<div><label>None of the above<input id= "answer5" name="ans" type="radio" onclick="getResults()" value="None of the above"></label></div></p>';
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So your question is:

"How to take three values from the array, and the 4th value needs to be the right answer."

Assuming that you know where is the right answer.

Here is a simple solution:

var options = new Set() //Set allows you to store unique values.
options.add(rightAnswerIndex); //add the right answer to the options
while (options.size < 4){
    options.add(getRandomNumber()) //use your code to get random number here
}

So, just implement getRandomNumber() as a function and you are good to go.

With this solution, the right answer will be the first in this set, if it's important to have it in the 4th place, you can just replace it.

Convert your set to an array, and access the index you need:

var arrayFromSet = [...options];
var correctAnswer = arrayFromSet[0];
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda