Algorithm Record Collection: Output is correct but not the test

Hello,

Tell us what’s happening:
I can not pass the first test but everything looks ok and I have no more ideas to pass it. Because I do not understand that is not correct. The test that is not validated:

After updateRecords(5439, "artist", "ABBA"), artist should be "ABBA"

It seems that the console is producing the correct result: "artist": "ABBA"
So, if someone spots my mistake, it would help me a lot!

Your code so far

// jshint esversion:6, asi:true

// Setup
const collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
}
// Keep a copy of the collection for tests
const collectionCopy = JSON.parse(JSON.stringify(collection))

// Only change code below this line
const updateRecords = (id, prop, value) => {
	const setArtist = () => collection[id][prop] = value
	const setTracks = () => collection[id][prop] = [value]
	const addValue  = () => collection[id][prop].push(value)
	const delProp   = () => delete collection[id][prop]
	
	if (prop && value) {
		if (collection[id][prop] && prop === 'tracks') {
			console.log('Update!',id,prop, value)
			addValue()
		}
		else if (prop === 'artist'){
			console.log('Set Artist!',id,prop, value)
			setArtist()
		}
		else {
			console.log('Set Tracks!',id,prop, value)
			setTracks()
		}
	}
	else if (prop  && collection[id][prop]) {
		console.log('Delete!',id,prop, value)
		delProp()
	}
    else{
		console.error('Error',id,prop, value)
	}
	return collection
}
  
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA")

Output:

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

Your browser information:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

Link to the challenge:
https://www.freecodecamp.org/challenges/record-collection

Take out your console.log calls.

It has to do with the fact you are using ES6 const when declaring collection, collectionCopy. The FCC tests use the variables in such a way, that you can not use const, because they result in errors which affect the tests. You will notice the original code has comments like:

// Only change code below this line

If you change those two const declarations back to var declarations, your code will pass.

2 Likes

Thanks ! Well seen, it works perfectly !
I used to adapt everything in ES6 and I had never been confronted with a case like this.
I would be more careful now !