Skip to content
Advertisement

JavaScript, What have I done wrong? [closed]

So, I’ve been messing around with this bit of code which I will eventually be moving into my mute command. I can’t seem to figure out what is wrong with this.

if(args[0].length >= 4) {
  return msg.reply("Please use abbreviated time formats! **m**, **h**, **d**, **w**")
}
var data = args[0]
if(args[0].length = 3) {
  data = data.substr(0, 2) + " " + data.substr(2, 3);
  msg.channel.send(data);
}
else if(data.length = 2) {
  data = data.substr(0, 1) + " " + data.substr(1, 2);
  msg.channel.send(data);
  msg.channel.send("test")
}

Basically, the first part where it sends a message if the length is 4 characters or more works. But, no matter how many characters below 4 are, it will always only run the if(args[0].length =3) part of the code. I’ve been trying to figure it out for the past hour and I don’t seem to know what is wrong with it. I must be blind or something.

Advertisement

Answer

This is an attempted assignment statement:

if(args[0].length = 3) {

The evaluation of that expression is not only an unattended assignment, but it always evaluates to 3 which means it will always be true. That’s why this clause always runs regardless of the length of your argument.

This is probably what you meant (comparison, not assignment):

if(args[0].length == 3) {

And even better:

if(args[0].length === 3) {

Same mistake needs a correction for the else if(data.length = 2) { clause as well.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement