freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../intermediate-algorithm-scri.../make-a-person.md

3.8 KiB

id title challengeType forumTopicId dashedName
a2f1d72d9b908d0bd72bb9f6 Fazer uma pessoa 5 16020 make-a-person

--description--

Preencha o construtor do objeto com os seguintes métodos abaixo:

getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)

Execute os testes para ver a saída esperada para cada método. Os métodos que recebem um argumento têm de aceitar apenas um argumento e tem de ser uma string. Estes métodos devem constituir o único meio de interação com o objeto.

--hints--

Nenhuma propriedade deve ser adicionada. Object.keys(bob).length deve sempre retornar 6.

assert.strictEqual(
  Object.keys((function () {
    let bob = new Person('Bob Ross');
    bob.setFirstName('Haskell');
    bob.setLastName('Curry');
    bob.setFullName('John Smith');
    return bob;
  })()).length,
  6
 );

bob instanceof Pessoa deve retornar true.

assert.deepEqual(bob instanceof Person, true);

bob.firstName deve retornar undefined.

assert.deepEqual(bob.firstName, undefined);

bob.lastName deve retornar undefined.

assert.deepEqual(bob.lastName, undefined);

bob.getFirstName() deve retornar a string Bob.

assert.deepEqual(bob.getFirstName(), 'Bob');

bob.getLastName() deve retornar a string Ross.

assert.deepEqual(bob.getLastName(), 'Ross');

bob.getFullName() deve retornar a string Bob Ross.

assert.deepEqual(bob.getFullName(), 'Bob Ross');

bob.getFullName() deve retornar a string Haskell Ross após bob.setFirstName("Haskell").

assert.strictEqual(
  (function () {
    bob.setFirstName('Haskell');
    return bob.getFullName();
  })(),
  'Haskell Ross'
);

bob.getFullName() deve retornar a string Haskell Curry após bob.setLastName("Curry").

assert.strictEqual(
  (function () {
    var _bob = new Person('Haskell Ross');
    _bob.setLastName('Curry');
    return _bob.getFullName();
  })(),
  'Haskell Curry'
);

bob.getFullName() deve retornar a string Haskell Curry após bob.setFullName("Haskell Curry").

assert.strictEqual(
  (function () {
    bob.setFullName('Haskell Curry');
    return bob.getFullName();
  })(),
  'Haskell Curry'
);

bob.getFirstName() deve retornar a string Haskell após bob.setFullName("Haskell Curry").

assert.strictEqual(
  (function () {
    bob.setFullName('Haskell Curry');
    return bob.getFirstName();
  })(),
  'Haskell'
);

bob.getLastName() deve retornar a string Curry após bob.setFullName("Haskell Curry").

assert.strictEqual(
  (function () {
    bob.setFullName('Haskell Curry');
    return bob.getLastName();
  })(),
  'Curry'
);

--seed--

--after-user-code--

if(bob){
  bob = new Person("Bob Ross");
}

--seed-contents--

const Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  this.getFullName = function() {
    return "";
  };
  return firstAndLast;
};

const bob = new Person('Bob Ross');
bob.getFullName();

--solutions--

const Person = function(firstAndLast) {

  let firstName, lastName;

  function updateName(str) {
    firstName = str.split(" ")[0];
    lastName = str.split(" ")[1];
  }

  updateName(firstAndLast);

  this.getFirstName = function(){
    return firstName;
  };

  this.getLastName = function(){
    return lastName;
  };

  this.getFullName = function(){
    return firstName + " " + lastName;
  };

  this.setFirstName = function(str){
    firstName = str;
  };


  this.setLastName = function(str){
    lastName = str;
  };

  this.setFullName = function(str){
    updateName(str);
  };
};

const bob = new Person('Bob Ross');
bob.getFullName();