-2

Could someone help me with this problem? i am trying to return random strings from an array. here is my code:

function getComputerChoice() {
  const Mystring = ['rock', 'paper', 'scissors'];
  const random = Math.floor(Math.random() * 3);
}

console.log(getComputerChoice());

I declared my variables: "Mystring" which contains the array of strings and "random" that contains the Math.random() function but am lost on the next steps. Would really appreciate if someones points me in the right direction.

5
  • 1
    Please read How to Ask, especially the section titled "Write a title that summarizes the problem" Commented Mar 10, 2023 at 15:50
  • 1
    Also, please see Why is "Can someone help me" not an actual question?. Tl;dr you have to show your work and ask a specific question. Also, what do you mean by "the final method"? Commented Mar 10, 2023 at 15:51
  • you forget a return MyString[random]; I think Commented Mar 10, 2023 at 15:52
  • 1
    Welcome to Stack Overflow! Please have a look around, and read through the help center, in particular How do I ask a good question? I also recommend Jon Skeet's Writing the Perfect Question and Question Checklist. Please search before posting. More about searching here. Commented Mar 10, 2023 at 15:52
  • @depperm thanks for the highlight! i see it now Commented Mar 10, 2023 at 16:06

1 Answer 1

0

Need to return Mystring[random]. Very slight coding practice improvement would be to use Mystring.length instead of 3.

function getComputerChoice() {
  const Mystring = ['rock', 'paper', 'scissors'];
  const random = Math.floor(Math.random() * Mystring.length);
  return Mystring[random]
}

console.log(getComputerChoice());

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.