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

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

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 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