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

170
Vistas
why is this code returning true instead of false

here is the code :

const str = "asdfcvb";
const result = str.split("").every((e) => {
    return e==='a'||'b'});
console.log(result);

I expect str.split("") returns an array, and every method check if every element in the array equals to 'a' or 'b', which should return false, but it returns true

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Because e==='a'||'b' means ((e==='a')||'b')

When the e !== 'a' case, it will return 'b' which is truthy in Javascript, so every method got truthy value from each term and then the result will be true.

You may use the follow to achieve what you want

const result = str.split("").every((e) => {
    return (e==='a') || (e==='b')
});

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can't omit the === from the second part of your condition. Right now what you're saying is either return e === 'a' or 'b'. When e is not equal to 'a' it returns 'b'. Which is truthy and when turned into a boolean it becomes true. Run this in the browser:

'a' === 'b' || 'c'

If you run it you see that it returns 'c'. The same happens in your callback. You have to change your return statement to this for it to work:

return e === 'a' || e === 'b'
about 4 years ago · Juan Pablo Isaza Denunciar

0

Everything seems correct. The problem is in the way you compared the strings

you should compare those like e === "a" || e === "b"

and the working code

const str = "asdfcvb";
const result = str.split("").every((e) => {
    return (e === "a" || e === "b")});
console.log(result);

Explanation:

return e==='a'||'b'

running the test for the first item which is "a" will return true as now e is "a" here, which is equal to "a".

now if it gets to second item which is "s" and obviously not equal to "a" so return e==='a'||'b' becomes return false||'b' and now when you evaluate this the returned value will be "b"

and putting "b" as a condition is similar to true cause it is not undefined or null

this can be tested as follows

if("b") return true; // returns true

Thus it returns true for every item thereby returning true at last

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