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

442
Vistas
Can someone check my solution for the Record Collection exercise on freeCodeCamp?

Here are the instructions.

Your function must always return the entire record collection object.

If prop isn't tracks and value isn't an empty string, update or set that album's prop to value.

If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.

If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.

If value is an empty string, delete the given prop property from the album. Note: A copy of the recordCollection object is used for the tests.

My solution was:

function updateRecords(records, id, prop, value) {
  return records;
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } else if (prop == "tracks" && records[id].hasOwnProperty("tracks") == false) {
    records[id][prop] = [value];
  } else if (prop == "tracks" && value !== "") {
    records[id][prop].push(value);
  } else if (value == "") {
    delete records[id][prop];
  }
  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Why didn't this work?

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

0

One possible solution to this task

const recordCollection={2548:{albumTitle:"Slippery When Wet",artist:"Bon Jovi",tracks:["Let It Rock","You Give Love a Bad Name"]},2468:{albumTitle:"1999",artist:"Prince",tracks:["1999","Little Red Corvette"]},1245:{artist:"Robert Palmer",tracks:[]},5439:{albumTitle:"ABBA Gold"}};

function updateRecords(records, id, prop, value) {
  const record = records[id];

  if (!value) {
    delete record[prop];
    return records;
  }

  if (prop === 'tracks') {    
    record[prop] ??= [];
    record[prop].push(value);
    return records;
  } 

  record[prop] = value;
  return records;
};

console.log(updateRecords(recordCollection, 5439, 'artist', 'ABBA')[5439]);
console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me")[5439]);
console.log(updateRecords(recordCollection, 2548, "artist", "")[2548])
console.log(updateRecords(recordCollection, 1245, "tracks", "Addicted to Love")[1245])
console.log(updateRecords(recordCollection, 2548, "tracks", "")[2548])
console.log(updateRecords(recordCollection, 1245, "albumTitle", "Riptide")[1245])
.as-console-wrapper {max-height: 100% !important; top: 0}

about 4 years ago · Juan Pablo Isaza Denunciar

0

the way to do that:
first create significant records for testing.

PS: using Logical nullish assignment (??=) greatly simplifies the code.

const
  rec0 = { 5439 : { prop1: "aaa" } }
, rec1 = { 5439 : { prop1: "aaa", artist: 'xyz' }  }
, rec2 = { 5439 : { prop1: "aaa", artist: 'xyz' }  }
, rec3 = { 5439 : { prop1: "aaa"                           }}
, rec4 = { 5439 : { prop1: "aaa", tracks: ['aa','bb','cc'] }}
, rec5 = { 5439 : { prop1: "aaa", tracks: ['aa','bb','cc'] }}
;

console.log( JSON.stringify( updateRecords( rec0,  5439, 'artist', 'ABBA' )))
console.log( JSON.stringify( updateRecords( rec1,  5439, 'artist', 'ABBA' )))
console.log( JSON.stringify( updateRecords( rec2,  5439, 'artist', ''     )))

console.log( JSON.stringify( updateRecords( rec3,  5439, 'tracks', 't1' )))
console.log( JSON.stringify( updateRecords( rec4,  5439, 'tracks', 't1' )))
console.log( JSON.stringify( updateRecords( rec5,  5439, 'tracks', ''   )))


function updateRecords(records, id, prop, value)
  {
  if (prop == 'tracks')
    {
    records[id].tracks ??= [];
    if (value.length)  records[id].tracks.push(value);
    else        delete records[id].tracks;
    }
  else if (value.length) 
    records[id][prop] = value;

  return records;
  }

 
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

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