Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda