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

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

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 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