How to create a JavaScript Object Property Excercise

I am working on exercises at https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection and have the following code.

function updateRecords(records, id, prop, value) {
    if (value===""){
        delete records[id][prop]

    }
    else if (prop==="tracks"&&value!==""){
        records[id][prop]+=value
    }
    else if(prop!=="tracks"&&value!==""){
        records[id][prop]=value
    }
    else if (prop==="tracks"&&records.id.hasOwnProperty("tracks")===false){
        records.id.tracks.value=value;
    }
    return records
}

When I execute the code

console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"))

the output is

 '5439': { albumTitle: 'ABBA Gold', tracks: 'undefinedTake a Chance on Me' }

How can I make this undefined go away?
I am open to suggestions and feedback.
Operating System Windows 11 Development Build. Browser Chrome Version 91.0.4472.164 (Official Build) (64-bit). Webstorm IDE.

records[id][prop]+=value to records[id][prop]=value

records[id][prop] is undefined at the start of the if, so using += you are just adding more text to the existing undefined “string” (“string”).

else if (prop==="tracks"&&value!==""){
        records[id][prop]=value
    }
1 Like

I updated my program but now I am getting a string instead of a list or array. What can I do?

function updateRecords(records, id, prop, value) {
    if (value===""){
        delete records[id][prop]

    }
    else if (prop==="tracks"&&value!==""){
        records[id][prop]=value
    }
    else if(prop!=="tracks"&&value!==""){
        records[id][prop]=value
    }
    else if (prop==="tracks"&&value!==""){
        records[id][prop]=value
    }
    return records
}

Output

 '5439': { albumTitle: 'ABBA Gold', tracks: 'Take a Chance on Me' }

How do I tell it to output a list?

value is a string, so you need to wrap it yourself in an array

I now have the following program but push is deleting the array instead of adding “Free” to the end of the array.

function updateRecords(records, id, prop, value) {

    if (prop!=="tracks"&&value!==""){
        records[id][prop]=value
    }

    else if (prop === "tracks" && 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
}

is returning

 '2468': { albumTitle: '1999', artist: 'Prince', tracks: [ 'Free' ] },

What is the issue with the program?
I am confused. I want to tell it to return an array with “Free” added on.

I think it’s just that this is always true, so the other condition with prop === "tracks" never execute