-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.

  • 1
    Please read [ask], especially the section titled "Write a title that summarizes the problem" – Heretic Monkey Mar 10 '23 at 15:50
  • 1
    Also, please see [Why is "Can someone help me" not an actual question?](https://meta.stackoverflow.com/questions/284236/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"? – David Makogon Mar 10 '23 at 15:51
  • you forget a `return MyString[random];` I think – Laaouatni Anas Mar 10 '23 at 15:52
  • 1
    Welcome to Stack Overflow! Please have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Please [**search**](/search?q=%5Bjs%5D+get+random+element+from+array) before posting. More about searching [here](/help/searching). – T.J. Crowder Mar 10 '23 at 15:52
  • @depperm thanks for the highlight! i see it now – Dafe Victor Mar 10 '23 at 16:06

1 Answers1

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());
depperm
  • 10,606
  • 4
  • 43
  • 67