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

326
Vistas
Why is the test with no arguments failing in Typescript?
export function twoFer( arg : string ): string 
{
  if( arg !== "")
  {
    return "One for " + arg + ", one for me.";
  }
    return "One for you, one for me." ;
}

I am not able to understand what am I doing wrong. This is failing in the case when no argument is supplied.

describe('TwoFer', () => {
  it('no name given', () => {
    const expected = 'One for you, one for me.'
    expect(twoFer()).toEqual(expected)
  })
})

Output:

TEST FAILURE
Error: expect(received).toEqual(expected) // deep equality

Expected: "One for you, one for me."
Received: "One for undefined, one for me."

https://exercism.org/tracks/typescript/exercises/two-fer

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

0

A string parameter not specified doesn't contain an empty string; it contains undefined. You would want something like this:

export function twoFer(arg?: string): string
{
    if(typeof arg !== "undefined") {
        return "One for " + arg + ", one for me.";
    }
    return "One for you, one for me.";
}

UPDATE:

Alternatively, you can make arg a parameter with a default value instead to reduce redundancy.

export function twoFer(arg = "you"): string
{
    return "One for " + arg + ", one for me.";
}
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