Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda