Last active
December 21, 2016 11:43
-
-
Save gustavo-rodrigues-dev/5355226 to your computer and use it in GitHub Desktop.
Sorteio da megasena
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//retorna true se o array conter o valor testado | |
Array.prototype.contains = function(obj) { | |
var i = this.length; | |
while (i--) { | |
if (this[i] == obj) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function random(i, f) { | |
if (i > f) { | |
numInicial = f;numFinal = i+1; | |
} else { | |
numInicial = i;numFinal = f+1; | |
} | |
numRandom = Math.floor((Math.random()*(numFinal-numInicial))+numInicial); | |
return numRandom; | |
} | |
//função mega sena, ela retorna um array com 6 numeros para o sorteio | |
var megasena = function (){ | |
this.result = []; | |
var num = 0; | |
do | |
{ | |
num = random(1,60); | |
//caso o array de resultado não contenha o valor randomico, incrementa o valor | |
if (!this.result.contains(num)){ | |
this.result.push(num); // +"" converte pra string, resolvendo o problema do type error ao chamar o resultado | |
} | |
} | |
while (this.result.length<6); | |
return this.result.sort(function(a, b){return a-b}); | |
} | |
console.log(megasena()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment