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

442
Visualizações
JavaScript: Possibly Asynchronous vs Synchronous For Loop issue? Or some other mistake?

Doing a JavaScript assignment: I need to create a game where the user tries to correctly guess 4 random numbers from 1-6. Ex. 1 3 2 4 or 1 6 5 3 or etc. The computer should analyze it and tell the player (a) how many of his or her guessed digits were correct, but in the wrong place, and (b) how many of the guessed digits were correct and in the right place.

Example below (bold = user inputs):

Welcome to Mastermind

Please enter your four numerical guesses (space separated): 2 4 3 1

You have 2 correct number(s) and 1 correct location(s).

Please enter your four numerical guesses (space separated): 4 5 3 2

Correct!

You are a MasterMind!

However in my code, where I count the number of correct numbers in wrong positions and right positions, the console outputs wrong numbers. I make copies of the correctAnswerList and userGuessList. So if the user guess matches with the correct answer, I change the numbers in these copies(outside of the 1-6 range) so they aren't recounted. I tried to search on google what could be my issue since I'm new to JavaScript. Read some things about async for loops but don't really get it. Code is below, thanks in advance!!

function checkingCorrect(userGuessList, correctAnswerList){
  var correctLocation = 0;
  var correctNumber = 0;
  var copyUserGuessList = userGuessList;
  var copyCorrectAnswerList = correctAnswerList
  
  for(var i = 0; i < 4; i++){
    if(userGuessList[i] == correctAnswerList[i]){
      correctNumber++
    }
    for(var x = 0; x < 4; x++){
      if(copyUserGuessList[i] == copyCorrectAnswerList[x]){
        copyUserGuessList[i] = -1;
        copyCorrectAnswerList[x] = 0;
      }
    }
    if(copyUserGuessList[i] == -1){
    correctNumber++; }
  }
  correctNumber = correctNumber - correctLocation;
  return([correctNumber, correctLocation]);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. To create copies of arrays, you can't simply reassign their reference (var copyUserGuessList = userGuessList;) and instead you should create new arrays which can be easily done by using the spread syntax within array brackets (var copyUserGuessList = [...userGuessList];).

  2. Incrementing of correctNumber at if(userGuessList[i] == correctAnswerList[i]){ correctNumber++ } should be replaced with the incrementing of correctLocation and result in if(userGuessList[i] == correctAnswerList[i]){ correctLocation++ }.

Your resultant code should look as follows:

function checkingCorrect(userGuessList, correctAnswerList){
  var correctLocation = 0;
  var correctNumber = 0;
  var copyUserGuessList = [...userGuessList];
  var copyCorrectAnswerList = [...correctAnswerList];
  
  for(var i = 0; i < 4; i++){
    if(userGuessList[i] == correctAnswerList[i]){
      correctLocation++
    }
    for(var x = 0; x < 4; x++){
      if(copyUserGuessList[i] == copyCorrectAnswerList[x]){
        copyUserGuessList[i] = -1;
        copyCorrectAnswerList[x] = 0;
      }
    }
    if(copyUserGuessList[i] == -1){
    correctNumber++; }
  }
  correctNumber = correctNumber - correctLocation;
  return([correctNumber, correctLocation]);
}
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