1

I'm working in a discord bot, specifically in a rate command, which is supposed to return:


"I would rate (thing to be rated) (random number)/10."


But it returns:


"I would rate (thing to be rated) [object Undefined]/10."

client.on('message', message => {
  if(message.content.startsWith (prefix + "rate")) {
    if(message.content.slice(prefix.length + 4) === ""){
      message.channel.send("Give me something to rate");
    }
    else
      message.channel.send("I would rate" + "**" + 
        message.content.slice(prefix.length + 4) + "**" + " " + 
        toString(Math.floor(Math.pow(Math.random, 10))) + "/10");
  }

What can possibly be wrong?

Taplar
  • 24,788
  • 4
  • 22
  • 35
InutiLuke
  • 15
  • 3
  • Also `Math.random()` is a method. You're missing the `()` to invoke it – Taplar Aug 15 '18 at 21:33
  • If you want a number between 0 and 10, don't use Math.pow() just multiply 10 on the random number generated. Math.random() * 10. Also wrong use of toString. Math.floor(Math.random() * 10).toString() – Wesgur Aug 15 '18 at 21:38
  • `toString` is not a function (unless you wrote it) ...`Math.pow` will raise some number to the power another number, (10 in your case), `Math.random` is a function that returns a value between 0 and 1, and any number between 0 and 1 raised to the power of 10 will become a smaller number between 0 and 1, so `Math.floor`, once you fix the `Math.random` thing, will always end up 0 – Jaromanda X Aug 15 '18 at 21:39
  • Possible duplicate of [Generate random number between two numbers in JavaScript](https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript) – André Aug 16 '18 at 09:50

1 Answers1

0

You need to incorporate the following code (considering you want round integers returned):

Math.floor(Math.random()*11) 

Ditch the Math.pow() method.

edit made from suggestion by @Geert-Jan

JKimbrough
  • 226
  • 1
  • 8
  • If OP wants integers in range `[0, 10]` inclusive the above is not correct, since 0 is never returned. Assuming integer values and an even distribution `Math.floor(Math.random() * 11)` works. Note: Math.random() returns a float in `[0,1)` so the above will never result in `11` – Geert-Jan Aug 15 '18 at 22:21
  • I'm not following your comment? Are you making a suggestion to my code? Sounds like you are just explaining that my code will return integers 0-10 randomly? Help me understand... – JKimbrough Aug 15 '18 at 22:25
  • 1
    Your code returns integers in range [1,10] randomly. I supplied code to return integers in range [0,10] randomly. Not entirely sure what OP wants though. – Geert-Jan Aug 15 '18 at 22:29
  • 1
    Oh wow - very careless mistake on my part - good catch. I will make the edit for OP. – JKimbrough Aug 15 '18 at 22:32