Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

332
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!