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

228
Visualizações
Why isn't my second 2 else if statement running?

I don't understand why my second else if statement never runs. Can someone explain it to me?

Name = window.prompt("Tell your First Name");

if (Name.endsWith('X') == true || Name.endsWith('x') == true) {
  meh = Name.slice(0, -1)
  console.log(meh)
} else if (Name.startsWith('X') == true || Name.startsWith('x') == true) {
  hello = Name.slice(1)
  console.log(hello)
} else if (Name.startsWith('X') == true || Name.startsWith('x') == true && Name.endsWith('X') == true || Name.endsWith('x') == true) {
  var result = Name.slice(1) && Name.slice(0, -1);
  console.log(result)
} else {
  console.log(Name)
}

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your code needs to start with most specific and go to the least. The code will not check all cases and figures out what is the best match, it starts from the top and first thing it matches it exits.

const firstName = window.prompt("Tell your First Name");

const endsWith = firstName.toUpperCase().endsWith('X');
const startsWith = firstName.toUpperCase().startsWith('X');

if (startsWith && endsWith) { console.log('starts and ends'); }
else if (startsWith) {console.log('starts'); }
else if (endsWith) {console.log('ends'); }
else {console.log('nope'); }

about 4 years ago · Juan Pablo Isaza Relatório

0

There are quite a few issues with your logic. For example:

  • You have a compound conition to check of a character in string but, you should change the string case and then determine if the character you are looking for "startsWith" or "endWith" which appears to be the only to senarios your concerned with.

  • secondly the "startsWith" and "endWith" functions natrually return a boolean value so equating the return with true or false would be redundant and uneccessary.

  • If else execution of one or other

  • If else if execute one or other - just enables the ability to take other senarios into consideration.

for example:

const Name = window.prompt("Tell your First Name");


if (Name.toUpperCase().endsWith('X')) {
  hello = Name.slice(1)
  console.log(hello)
} else if (Name.toUpperCase().startsWith('X')) {
  var result = Name.slice(1) && Name.slice(0, -1);
  console.log(result)
} else {
  console.log(Name)
}

is the same above but it will execute twice because the block has been broken into two

const Name = window.prompt("Tell your First Name");


if (Name.toUpperCase().endsWith('X')) {
  hello = Name.slice(1)
  console.log(hello)
}

if (Name.toUpperCase().startsWith('X')) {
  var result = Name.slice(1) && Name.slice(0, -1);
  console.log(result)
} else {
  console.log(Name)
}

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. You should move startsWithX && endsWithX to be first condition, otherwise one of above will take it's place
  2. Convert your string to lowercase (or uppercase) and remove need for double check of X and x

Name = window.prompt("Tell your First Name").toLowerCase();

console.log('Answer: ', Name);

if (Name.startsWith('x') == true && Name.endsWith('x') == true) {
  var result = Name.slice(1) && Name.slice(0, -1);
  console.log('Starts and ends with X', result)
} else if (Name.endsWith('x') == true) {
  meh = Name.slice(0, -1)
  console.log('Ends with X', meh)
} else if (Name.startsWith('x') == true) {
  hello = Name.slice(1)
  console.log('Starts with X', hello)
} else {
  console.log('Default', Name)
}

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