Home > Software design >  Math.random not working, keeps choosing number 1 instead of random number
Math.random not working, keeps choosing number 1 instead of random number

Time:04-27

So i have setup a command that is meant to find a random number, put that number to a file and then get the text from that file. It works but for some reason line 13 keeps on choosing number 1 all the time. Here is a code sample of the whole command, can someone please help me find out why it isn't choosing numbers other than 1?

const discord = require("discord.js");
const msg = require('../../JSON/msg.json');
const power = require('../../JSON/power.json');
const advice = require('../../JSON/advice.json');
var result = 456;

module.exports = {
  name: "ranmsg",
  category: "roleplay",
  description: "gives the user a random msg from a list",
  run: async (client, message, args) => {

result = Number(Math.floor(Math.random() * 3));
const msgc = msg[Math.floor(Math.random() * msg.length)];
const powerc = power[Math.floor(Math.random() * power.length)];
const advicec = advice[Math.floor(Math.random() * advice.length)];
if (result = 0){
  let embed = new discord.MessageEmbed()
  .setTitle("Random Message:")
  .setDescription(`${msgc}`)
  .setColor("RANDOM")
  .setAuthor(message.author.tag, message.author.avatarURL())
  .setFooter(`Thank you for choosing **Aurasouls**`)

  return message.channel.send(embed)
}
else if (result = 1){
  let embed2 = new discord.MessageEmbed()
  .setTitle("Random Superpower:")
  .setDescription(`${powerc}`)
  .setColor("RANDOM")
  .setAuthor(message.author.tag, message.author.avatarURL())
  .setFooter(`Thank you for choosing **Aurasouls**`)

  return message.channel.send(embed2)
}
else if (result = 2){
  let embed3 = new discord.MessageEmbed()
  .setTitle("Random Advice:")
  .setDescription(`${advicec}`)
  .setColor("RANDOM")
  .setAuthor(message.author.tag, message.author.avatarURL())
  .setFooter(`Thank you for choosing **Aurasouls**`)

  return message.channel.send(embed3)
}
else{
  return message.reply("Error, please try again.")
}
message.reply(result)
 }
}

CodePudding user response:

As Dave mentioned, = is assignment, == is equality. If you do if(result = 1) it will be every time true because result will be a number different from 0. You should do if(result == 1) and so on

CodePudding user response:

Line 16 and similar lines assign a value to result. Wherever you have "if (result=N) { ..." you should have "if (result===N) {..."

  • Related