Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda