0

I am trying to learn some simple things in js, so what i am trying to achieve is every time I get a random number I want to add it inside an array at he front, then compare the first two values make sure they are not [6,6]

function randomNumber() {
  number = Math.floor((Math.random() * 6) + 1);
  return number;
}

function addValue() {
  var score = [];
  score.unshift(randomNumber());
  console.log(score);
  return score;
}

At the moment what is happening, every time i call the randomNumber the array only stores one value, it does not append it to the list and overweights the previous.

What am I doing wrong and why is it working? thx

1
  • Why do you care about the first 2 numbers matching, anyways? Commented Oct 31, 2017 at 23:44

1 Answer 1

2

the variable you use to store the array needs to be created and passed through the argument list, or stored globally. when you do var x = [] you are create a NEW empty array.

  var score = [];
  
  addValue(score);
  addValue(score);
  addValue(score);

function randomNumber() {
  number = Math.floor((Math.random() * 6) + 1);
  return number;
}

function addValue(score) {

  score.unshift(randomNumber());
}

console.log(score)

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

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.