0

I am trying to choose a random word from an array of strings but cannot figure out how.

I have researched on this and here is what I have so far:

roast.js
const roastList = [
    'Apples',
    'Bananas',
    'Pears',
  ];

  const roast = roastList[Math.floor(Math.random() * roastList.length)];

module.exports = {
    roast
};
index.js:
case 'roast':
        if (!message.mentions.users.size) {
            return message.reply('you need to tag a user in order to roast them!');
        }

        message.channel.send(`Hey ${taggedUser}, ${roast}`);
        break;

I copied the code so the fruits are placeholders. I hope that I can get different fruits each time I output roast.

1
  • Please edit your post to include the code that consumes the roast export. Commented Apr 5, 2019 at 0:27

3 Answers 3

1

The problem here is that the module code will only be run one time, that means that only one "roast" will be chosen the first time the module is loaded. You want to wrap up the "roast" selection logic in a function that can be called every time you want to "roast" someone.

Consider making roast.js export a function:

const roastList = [
    'Apples',
    'Bananas',
    'Pears'
];

module.exports = function () {
    return roastList[Math.floor(Math.random() * roastList.length)];
};

Then call that function in your template:

const roast = require('./roast.js');
...
message.channel.send(`Hey ${taggedUser}, ${roast()}`);
Sign up to request clarification or add additional context in comments.

Comments

1

Works for me. With the export you have defined in your example the import can be done like the following, for example.

const roast = require('./roast').roast;

If you simply export the string instead of an object containing the string, you can also do this as follows:

module.exports = roast;
...

const roast = require('./roast');

See also my repl.it for demonstration

EDIT: I have just noticed by the comments you have posted that you are probably looking for a solution where you can get a new random roast each time when a roaster is required in the chat protocol. To do this my suggestion is to export a function which returns a random roast string. See example below, I have also extended the repl.it demo.

roast3.js

const roastList = [
    'Apples',
    'Bananas',
    'Pears',
  ];

function roast() {
  return roastList[Math.floor(Math.random() * roastList.length)]
}

module.exports = {
  roast
};

index.js

const roast3 = require('./roast3').roast;
for (x=0; x<10; x++) {
  console.log(roast3())
}

2 Comments

I only get the output, [object Object] as the output when using this code.
Did you copy the code verbatim? Then it should work as shown in my repl.it. Note, roast3 is function. So you need to make a function call when using it, e.g., console.log(roast3()) or ${roast3()}
0
const roastList = [
  'Apples',
  'Bananas',
  'Pears',
];

const roast = roastList[Math.floor(Math.random() * roastList.length)];
console.log(roast);

This actually works. But why do you exports it ?

3 Comments

I import it to index.js and use it there. Just keeps the code a bit more clean and tidy.
imgur.com/a/wS8Wb54 is proof it only gives one output. imgur.com/a/nRdNsHi This is the code in index.js
@OscarHurst, see EDIT on my answer

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.