Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
JavaScript, What have I done wrong?

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

A single = is assign a value to a variable.

const name = 'John'
let age = 30

Double = is a comparison operator. It will transform the value to the same type before comparison.

console.log('1' == 1) // true

Triple = does the same thing but it won't transform the type. So the result is more accuracy.

console.log('1' === 1) // false

In most circumstances, stick with the triple =.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!