Problems with array "Uncaught Referenceerror: Function is not defined"

Asked

Viewed 313 times

1

Beauty guys? It happens the following, I created a tool to generate gambling (quina, mega sena and etc. link tool: generaterandom.000webhostapp.com)

In the attached image we see the part where the algorithm puts 60 random numbers in an array Then it is traversed the entire array to check if a number left repeated, if it was repeated, all 60 numbers are generated again, this is making it very heavy and this recursion is getting in the way of a lot, crashing and returning me "Uncaught Referenceerror: Function is not defined"

I wanted to know if it is possible to optimize this part of the code, with only one for, generate 1 number, see if it is in the array and add, would be fewer lines, less for and I believe that would not lock, but I can not do this optimization in the lines, I did but it didn’t work out... can someone guide me? whole fuction link: generaterandom.000webhostapp.com/gerarJogos.js

for (var i = 0; i < comprimento; i++){
    vet[i] = Math.floor(Math.random() * 60 + 1);
}

for (var z = 0; z < comprimento; z++){
   for(var y = 0; y < comprimento; y++){
       if(z !== y){
          if(vet[z] === vet[y]){
             gerarJogos();
    } 
  }
}

2 answers

6


You can make a while where the condition to exit the loop is that the array reaches a length of X elements. Inside this while you can use Array.includes and test if the number already exists.

Example:

const comprimento = 5
let numeros = []

while (numeros.length < comprimento) {
  let random = Math.trunc(Math.random() * 60 + 1)
  
  if(!numeros.includes(random)) {
    numeros.push(random)
  } else {
    console.log(`Número repetido, tentando novamente (${random})`)
  }
}

console.log(numeros)

  • Perfect face, perfect !! but I didn’t understand the last line "numbers = .Sort((a, b) => a - b)"

  • I just sorted the numbers to make it easier to read. I’ll update the answer to explain how Sort works

  • but what is a ? and what is b? and will always be a, b?

  • a and b are the arguments received in the comparison function. These are names that I have chosen, may be what you think best. Read the documentation Linkei in the answer you will understand better

  • Let me get this straight: [0, 3, 1, 2, 4] a is 0, b is 3 0 < 3 = so 0 is first then a is 3, b is 1 3 > 1 = so 1 is second after a is 1, b is 2 1 < 2 = so 2 is third then a is 2, b is 4 2 < 4 = then 3 is fourth

  • but it did not make sense, the 3 was outside the ordination

  • Why is this symbol more equal (=>) and less between A and B? You have to subtract the elements? This doesn’t make sense

  • I mention in the answer: "If the result of the comparison function is...". Who will implement an sorting algorithm is the browser itself, you are just telling the browser how to consider things bigger or smaller compared to themselves.

  • And the symbol => is an Arrow Function, which in this case would be equivalent to: [0, 3, 1, 2, 4].sort(function(a, b) { return a - b})

  • In fact the issues are polluting the answer and making it confusing. I will remove the explanation and the code that generated the confusion, after all it is not useful to the objective of the algorithm. Any doubt when the Sort function is just see in the documentation or open a specific question for it.

  • I created another question, please help me https://answall.com/questions/355661/doubts-sobre-array-sort

Show 6 more comments

3

Another alternative to this problem would be to use a data structure called set, its two main features are not allow duplicated elements and is not a classifieds collection.

Logic is simplified in this way:

let sorteio = new Set();

while(sorteio.size < 6) sorteio.add(Math.trunc(Math.random() * 60 + 1));
  • It would be interesting to use a Math.round() before adding the number

  • I find more interesting the Math.trunc(), otherwise the case of giving 61 as a result.

  • @fernandosavio, good, I hadn’t thought of that

  • @fernandosavio added the Math.trunc() thanks for the correction :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.