1

I read about a loot box system and wanted to make my own kind of lucky number calculator.

I have 2 arrays, one with regular numbers and one with the winning ones. Two numbers will be put in a variable and if both these numbers are in the winning array you win!

Now the thing i'm struggling with is that when i make the numbers random my if else statement doesn't work anymore. It will always say false even though the numbers are correct.

if you declare the variables not random, it does work ( it is shown in the code below).

How can i make the if else statement working with the random generator?

CODE:

function go(){

var Numbers = ['one', 'two', 'three','four','five'];
var LuckyNumbers = ['three', 'four', 'seven']

var num1, num2;

num1 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1);//This doesnt work..
num2 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1);//This doesnt work..

// num1 ="three"; -> this works but it is not random
// num2 ="four";  -> this works but it is not random

if([num1, num2].every(item => LuckyNumbers.includes(item))) { //always false when using the randoms.
  console.log("yep")
} else{
  console.log('nope') 
}

}

7
  • Just out of curiosity - why use strings 'one' instead of numbers 1? Commented Jun 4, 2018 at 19:56
  • 1
    splice removes the number from the array... Commented Jun 4, 2018 at 19:59
  • because now i want to try it with numbers, in a later project there will be other things as well :) Commented Jun 4, 2018 at 20:00
  • @gabi lee,But is doesn't effect the LuckyNumbers array right? Commented Jun 4, 2018 at 20:01
  • It isn't affecting LuckyNumbers because he never call splice on LuckyNumbers Commented Jun 4, 2018 at 20:07

3 Answers 3

1

This isn't related to your if statement at all. num1 and num2 are not storing what you think they're storing in your code as written.

Array.prototype.splice returns the removed elements in a new array. Even if there is only one, you'll need to reference it at index 0:

var Numbers = ['one', 'two', 'three','four','five'];
var LuckyNumbers = ['three', 'four', 'seven']

var num1, num2;

num1 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1)[0];
num2 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1)[0];

if([num1, num2].every(item => LuckyNumbers.includes(item))) {
  console.log("yep")
} else{
  console.log('nope') 
}

Edit: In the interest of giving you a more complete answer-- note that using splice here is not nearily as much of a problem as others seem to indicate. Yes, it muates the Numbers array, but the Numbers array is created anew every time you invoke your go function.

In this case, it's a completely reasonable way to solve this problem, though if you ever want to refactor your code such that Numbers and LuckyNumbers are stored in some parent scope, you'll need to be aware of the mutations:

const Numbers = ['one', 'two', 'three','four','five'];
const LuckyNumbers = ['three', 'four', 'seven']

function go() {
    // Slice with no arguments creates a clone of an array...
    var numbers = Numbers.slice();

    // Note I'm calling splice on `numbers` with a small `n` here...
    var num1 = numbers.splice(Math.floor(Math.random() * Numbers.length), 1)[0];
    var num2 = numbers.splice(Math.floor(Math.random() * Numbers.length), 1)[0];

    if([num1, num2].every(item => LuckyNumbers.includes(item))) {
      console.log("yep")
    } else{
      console.log('nope')
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

actually, in part is it related to the if. because hes referencing comparing [ ['two'], ['four'] ] and not ['two', 'four']. But your point is also valid as well. just thought id mention.
True. I felt it would be clearer to indicate that the problem lies with num1 and num2 not storing what the OP thinks they are storing, based on the semantics of variable names. One could change the if statement instead, but that would be a readability fail.
actually i thought exactly the same as you did initially, but as it turns out the only problem with his answer is nested array.every check. his mutating the correct array in this case that wont cause problems with his end answer. funny how at first glance we all looked at it differently!
I got i to work, Awesome! Really helpfull stuff guys. Many thanks ^^ Not sure what the mutagions have for effect, i'm quite new to JS but i will go and read about it.
0

Splice mutates the arrays and returns an array - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Just use an index instead if you don't care about repetition, or use the first element.

function go(){

var Numbers = ['one', 'two', 'three','four','five'];
var LuckyNumbers = ['three', 'four', 'seven']

var num1, num2;

// with repetitions
num1 = Numbers[Math.floor(Math.random() * Numbers.length)];
num2 = Numbers[Math.floor(Math.random() * Numbers.length)];

//without repetitions
num1 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1)[0];
num2 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1)[0];

if([num1, num2].every(item => LuckyNumbers.includes(item))) { //always false when using the randoms.
  console.log("yep")
} else{
  console.log('nope') 
}
}

3 Comments

Isn't there a chance i would get double numbers now?
two of the same numbers COULD occur here, but your case would still pass.
@walkingipad5 - you're right, I'll edit the answer. I didn't understand the question.
0

The array your using to compare against is actually a nested array of arrays it needs to be flat. ex: [['five'],['one']] couldnt be compared the way you were doing it. Hope this helps.

function go () {
  var Numbers = ['one', 'two', 'three', 'four', 'five']
  var LuckyNumbers = ['three', 'four', 'seven']

  var num1, num2

  num1 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1)
  num2 = Numbers.splice(Math.floor(Math.random() * Numbers.length), 1)

  if (flatten([num1, num2]).every(item => LuckyNumbers.includes(item))) {
    // always false when using the randoms.
    console.log('yep')
  } else {
    console.log('nope')
  }
}
go()

function flatten (arr) {
  return arr.reduce((accum, curr) => {
    if (Array.isArray(curr)) {
      return accum.concat(flatten(curr))
    } else {
      return accum.concat(curr)
    }
  }, [])
}

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.