1

I am having an issue with a simple logic in JavaScript, that checks for digits occurrence in an arrays. The problem is defined as follows:

No match 0

Make certain that your application accommodates repeating digits. For example, if a user guesses 1, 2, and 3, and the randomly generated digits are 1, 1, and 1, do not give the user credit for three correct guesses - just one. Save the file as Lottery.

The main issue is happening at if (lotteryResults.includes(guesses[j])). I don't know why it is working when I hard code a value in the includes.

2
  • 1
    You said you are having an issue and where it is occurring. But you did not mention what issue you are facing! Commented Apr 2, 2019 at 11:55
  • Despite guessing the correct number(s), it is still showing me won zero Commented Apr 2, 2019 at 11:58

4 Answers 4

2

The problem is that prompt() returns a string that should be converted to number. You can use + to convert it to number

for (var i = 1; i <= TIMES; i++) {
  var userInput = +prompt("Enter a guess");
  guesses.push(userInput);
}

And second problem is that you are comparing arrays guesses == lotteryResults. This will never return true. You can use join() and then compare

if (guesses.join('') == lotteryResults.join(''))

var TIMES = 3;

var userFirstGuess;
var userSecondGuess;
var userThirdGuess;

var guesses = [];

var firstRandom = Math.floor(Math.random() * 3) + 1;
var secondRandom = Math.floor(Math.random() * 3) + 1;
var thirdRandom = Math.floor(Math.random() * 3) + 1;

for (var i = 1; i <= TIMES; i++) {
  var userInput = +prompt("Enter a guess");
  guesses.push(userInput);
}

alert(guesses);

var winTen = 10;
var WinHun = 100;
var winThoun = 1000;
var winMillion = 10000;
var winZero = 0;

var lotteryResults = [];
lotteryResults.push(firstRandom);
lotteryResults.push(secondRandom);
lotteryResults.push(thirdRandom);

if (guesses.join('') == lotteryResults.join('')) {
  alert("You have won: $ " + winMillion);
} else {
  var matchCount = 0;
  for (var j = 0; j < guesses.length; j++) {
    if (lotteryResults.includes(guesses[j])) {
      matchCount++
      var index = lotteryResults.indexOf(guesses[j]);
      lotteryResults.splice(index);
      alert('Lottery ' + lotteryResults);
    }
  }

  switch (matchCount) {
    case 0:
      alert("You have won: $ " + winZero);
      break;
    case 1:
      alert("You have won: $ " + winTen);
      break;
    case 2:
      alert("You have won: $ " + winHun);
      break;
    case 3:
      alert("You have won: $ " + winThoun);
      break;
  }
}

Sign up to request clarification or add additional context in comments.

Comments

2

Answering your current problem, the prompt writes entries of type string into guesses. And after that you're trying to find a number inside.

What I suggest is to parseInt the guesses[j]

if (lotteryResults.includes(parseInt(guesses[j])) {

Another issue I found is

if (guesses == lotteryResults) {

You can't compare arrays like that. You might try

if (guesses.join('') === lotteryResults.join('')) {

EDIT Example:

console.log([7,3,1] === [7,3,1])
console.log([7,3,1].join('') === [7,3,1].join(''))

1 Comment

Sorry I was wrong. We can't compare arrays. Thanks for telling +1
1

Here var userInput = prompt("Enter a guess"); console.log(typeof userInput) // returns string. So when you are checking lotteryResults.includes(guesses[j]) it returns false for example lotteryResults.includes("4") // returns false.

You can fix your code as :

var userInput = prompt("Enter a guess"); guesses.push(Number(userInput));

OR

var userInput = prompt("Enter a guess"); guesses.push(parseInt(userInput));

Comments

0

you have to change the prompt values to Numbers just add + before gusses[j] try my code:

  for (var j = 0; j < guesses.length; j++) {
    if (lotteryResults.includes(+guesses[j])) {
      matchCount++
      var index = lotteryResults.indexOf(+guesses[j]);
      if(index !== -1){
      lotteryResults.splice(index,1);
      }
      alert('Lottery ' + lotteryResults);
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.