G
G
Godiluck2021-04-13 14:33:09
JavaScript
Godiluck, 2021-04-13 14:33:09

Why doesn't the code pass the test?

Hello, there was a problem while writing the code. There is a script above, below is a test. So, he swears at the line last: str2[0]. I would be grateful if you look and help to understand
The code works like this: it checks whether the user correctly filled in the first and last name fields. If everything is in 1 line, the object is split into an array and different elements are assigned to first and last respectively

export interface ICleverName {
  first?: string;
  last?: string;
}

export class CleverUserConverters {
  static ParseUserName(name: ICleverName): Required<ICleverName> {
    let str1 = name.first?.split(' ');
    let str2 = name.last?.split(' ');

    return {
      first: str1[0],
      last: str2[0],
    };
  }
}


describe('AppController', () => {
  it('should parse first and last names correctly', () => {
    const name1: ICleverName = { first: '1', last: '2' };
    const name2: ICleverName = { first: '1 2' };
    const name3: ICleverName = { last: '1 2' };

    const parsedName1 = CleverUserConverters.ParseUserName(name1);
    const parsedName2 = CleverUserConverters.ParseUserName(name2);
    const parsedName3 = CleverUserConverters.ParseUserName(name3);

    expect(parsedName1.first).toEqual('1');
    expect(parsedName1.last).toEqual('2');
    expect(parsedName2.first).toEqual('1');
    expect(parsedName2.last).toEqual('2');
    expect(parsedName3.first).toEqual('1');
    expect(parsedName3.last).toEqual('2');
  });
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question