Christopher Altamirano
Christopher Altamirano

Reputation: 157

How do I return or console.log my function to return my array of choice?

I am practicing my JavaScript and I am just trying to make a simple rock paper scissors game, I followed a little guide, but when I console.log it, it just prints the function not the array of choices. How do I get those choices?

let choice = ['Rock', 'Paper', 'Scissors'];

function computerPlay () {
    return choice[Math.floor(Math.random * choice.length)];
}

console.log(computerPlay);

Upvotes: 0

Views: 105

Answers (1)

Nidhin Joseph
Nidhin Joseph

Reputation: 10237

You need to make 2 changes for your code to run

  1. Change Math.random to Math.random()
  2. Change console.log(computerPlay) to console.log(computerPlay())

You need to call a function using fn_name() to execute it.

let choice = ['Rock', 'Paper', 'Scissors'];

function computerPlay() {
  return choice[Math.floor(Math.random() * choice.length)];
}

console.log(computerPlay());

Upvotes: 2

Related Questions