Skip to content

Instantly share code, notes, and snippets.

@cmendesce
Created November 22, 2019 22:08
Show Gist options
  • Select an option

  • Save cmendesce/bda67d2bd63ac459f5716dd6ae2ae760 to your computer and use it in GitHub Desktop.

Select an option

Save cmendesce/bda67d2bd63ac459f5716dd6ae2ae760 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script>
function clickSortear() {
document.getElementById("resultado").innerHTML = "";
var alunos = [];
var min = parseInt(document.getElementById("min").value);
var max = parseInt(document.getElementById("max").value);
alunos.push(document.getElementById("aluno1").value);
alunos.push(document.getElementById("aluno2").value);
alunos.push(document.getElementById("aluno3").value);
var questoes = [];
for (var i = 0; i < alunos.length; i++) {
var aluno = alunos[i];
if (aluno == '') {
continue;
}
var questao = sortearQuestao(min, max);
console.log(questao)
while (jaSorteada(questoes, questao)) {
questao = sortearQuestao(min, max);
}
questoes.push(questao);
mensagem = "aluno " + aluno + " apresenta questão " + questao;
document.getElementById("resultado").appendChild(criarItemLista(mensagem));
}
}
function criarItemLista(texto) {
var node = document.createElement("LI");
var textnode = document.createTextNode(texto);
node.appendChild(textnode);
return node;
}
function jaSorteada(questoes, questao) {
for (var j = 0; j < questoes.length; j++) {
if (questao == questoes[j]) {
return true;
}
}
return false;
}
function sortearQuestao(min, max) {
var random = Math.random() * (+max - min) + +min;
return parseInt(random);
}
</script>
</head>
<body>
<div>
<label for="aluno1">Intervalo de questões: de </label>
<input type="number" id="min" value="1" style="width: 30px;" /> a <input type="number" style="width: 30px;" value="11" id="max" />
</div>
<div>
<label for="aluno1">Aluno 1: </label>
<input type="text" id="aluno1" />
</div>
<div>
<label for="aluno2">Aluno 2: </label>
<input type="text" id="aluno2" />
</div>
<div>
<label for="aluno3">Aluno 3: </label>
<input type="text" id="aluno3" />
</div>
<input type="button" value="Sortear" onclick="clickSortear()" />
<ul id="resultado">
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment