0

I am trying to get a random element within an array, inside an object. I want to reuse that random element for another property in an object.

The problem is the function is trying to generate a random number again every time I call that.

For example, the object.randomWord() will return a different result than the object.wordSplit().

How to make sure all properties reference the same random element?

Thanks.

var object = {
  wordArray: ["falconheavy", "roadster", "tesla", "openai"],
  randomWord: function() {
    return this.wordArray[Math.floor(Math.random() * this.wordArray.length)];
  },
  wordSplit: function() {
    return this.randomWord().split('');
  },
}
console.log(object.randomWord()); //"falconheavy"
console.log(object.wordSplit()); // ["t","e","s","l","a"]

2
  • What have you tried so far? LocalStorage, cookies or simply a variable in the object come to mind Commented May 2, 2018 at 17:49
  • Why don't you save the randomWord to a variable, and then split that? Commented May 2, 2018 at 17:50

2 Answers 2

1

One way, In your code you can also do like this

DEMO

var object = {
  wordArray: ["falconheavy", "roadster", "tesla", "openai"],
  randomWord: function() {
    this.randomNo = this.wordArray[Math.floor(Math.random() * this.wordArray.length)];
    return this.randomNo;
  },
  wordSplit: function() {
    return (this.randomNo||'').split('');
  }
}

console.log(object.randomWord()); 
console.log(object.wordSplit());
.as-console-wrapper {max-height: 100% !important;top: 0;}

Another way, As you use to method randomWord() and wordSplit(). Inside of wordSplit() you again getting random number so instead of this you can first get random number and store in variable and again pass that value into wordSplit() for splitting.

DEMO

var object = {
  wordArray: ["falconheavy", "roadster", "tesla", "openai"],
  randomWord: function() {
    return this.wordArray[Math.floor(Math.random() * this.wordArray.length)];
  },
  wordSplit: function(v) {
    return v.split('');
  }
}
let result = object.randomWord();
console.log(result); 
console.log(object.wordSplit(result));
.as-console-wrapper {max-height: 100% !important;top: 0;}

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

1 Comment

This is exactly what I'm looking for. I want to call the object property without storing it to a new variable outside the object. Thanks for the help.
1

I would simply store the result of calling randomWord in a variable, and pass that as an argument to wordSplit, like so:

var object = {
  wordArray: ["falconheavy", "roadster", "tesla", "openai"],
  randomWord: function() {
    return this.wordArray[Math.floor(Math.random() * this.wordArray.length)];
  },
  wordSplit: function(word) {
    return word.split('');
  },
}

var randWord = object.randomWord();

console.log(randWord);
console.log(object.wordSplit(randWord));

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.