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

183
Vistas
How to add property to object inside another function?

I've been struggling with this exercise.

addCharacter: function (name, lastName, house, dateOfBirth, isMuggle)
{
    let id = 0;
    for (let i = 0; i < houses.length; i++) {
      if (house === houses[i]) {
        id = i + 1;
      }
    }
    if (id === 0) {
      return null;
    }
    const date = dateOfBirth.split("-").reverse().join("-");
    const d = new Date(date);
    const year = d.getFullYear();

    const character = {
      name,
      lastName,
      houseId: id,
      dateOfBirth,
      yearOfBirth: year,
      isMuggle,
      wand: {},
      spells: [],
    };

    characters.push(character);
    return character;
}

I need to add properties to wand object with this format {wood: wood, core: core, length: length}

addWand: function (name, wood, core, length) {
    const character = characters.find((character) => character.name === name);

    if (!character) return [];

    if (character.wand !== undefined) {
      return null
    }
}

Been trying with dot notation, assign method and spread operator but I can't make it work. Thanks in advance.

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

0

Is this what you're looking for?

addWand: function (name, wood, core, length) {
    const character = characters.find((character) => character.name === name);

    if (!character) return [];

    if (character.wand == undefined) {
      // 'wand' property doesn't exist, so create it from wood, core, and length:
      character.wand = { wood, core, length }
    } else {
      // 'wand' property exists, so *add* wood, core and length:
      character.wand = { ...character.wand, wood, core,length }
    }
}

If characters are always created with a wand property (as seems to happen with your addCharacter function), there is no need to check for if (character.wand == undefined) {. In this case the addWand code can be simplified to:

addWand: function (name, wood, core, length) {
    const character = characters.find((character) => character.name === name);

    if (!character) return [];

    // 'wand' property exists, so *add* wood, core and length:
    character.wand = { ...character.wand, wood, core,length }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Normal Assignment should be working. Find the snippet below:

let characters = [];
let houses = ["big", "small", "medium"];
let addCharacter = (name, lastName, house, dateOfBirth, isMuggle) => {
  let id = 0;
  for (let i = 0; i < houses.length; i++) {
    if (house === houses[i]) {
      id = i + 1;
    }
  }
  if (id === 0) {
    return null;
  }
  const date = dateOfBirth.split("-").reverse().join("-");
  const d = new Date(date);
  const year = d.getFullYear();

  const character = {
    name,
    lastName,
    houseId: id,
    dateOfBirth,
    yearOfBirth: year,
    isMuggle,
    wand: {},
    spells: [],
  };

  characters.push(character);
  return character;
}

let addWand = function(name, wood, core, length) {

  const character = characters.find((character) => character.name === name);
  if (!character) return [];

  if (character.wand === undefined) {
    return null
  } else {
    character.wand = {
      "wood": wood,
      "core": core,
      "length": length
    };
  }
  
}

addCharacter("john", "doe", "big", "18/9/1994", true);
addWand("john", "123", "12", "12");
addCharacter("misty", "doe", "big", "18/9/1994", true);
addWand("misty", "1234", "123", "123");
console.log(characters);

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