user19294301
user19294301

Reputation: 13

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]);
}

Upvotes: 0

Views: 53

Answers (1)

Ovidijus Parsiunas
Ovidijus Parsiunas

Reputation: 2732

  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]);
}

Upvotes: 0

Related Questions