Tell us what’s happening:
I added a property to an object and tested if this property is added inside of the code (right before i return the object). when i checked back the tests (test number 2) i found out that the property is not added for no reason (i have been staring for hours in it to check why it acted that way but to no avail)
Your code so far
// Setup
var collection = {
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'
}
};
// Only change code below this line
function updateRecords(object, id, prop, value) {
//print Input
console.log("INPUT: ID: " + id + " & prop: " + prop + " & value: " + value);
//check if the object is null
if (object ==null){
console.log("object is null");
return false;
}
//check if id is not a number
if (id == null || isNaN(id)){
console.log("Id is not a number");
return false;
}
//check if prop is not a string
if (prop == null || prop == undefined || typeof prop != 'string'){
console.log("prop is not a track");
return false;
}
//check if the value is not a string
if (value == null || typeof value != 'string' || value === ""){
console.log("value is not valid word");
return false;
}
//for variable cases of prop (albumTitle, atrist, tracks)
switch(prop){
//albumTitle & artist are of type string
case "albumTitle":
case "artist":
object[id][prop] = value;
break;
//tracks are of type string[]
case "tracks":
//if <obj> does not have <prop> then add an array property with <prop>
if (!object[id].hasOwnProperty(prop)){ //returns true in case 2
object[id].prop = {value};
} else {
//add <value> to the last index in <prop>>
object[id][prop].push(value);
}
break;
}
// console.log(object[id].prop) //this proves that the track has been added successfully
return object;
}
//Test Passed
// console.log(1)
// var one = updateRecords(collection, 5439, "artist", "ABBA") // artist should be ABBA
// console.log(one[5439].artist);
//Test Passed
// console.log("1_1")
// var one_one = updateRecords(collection, 5439, 'artist', 'ABBA'); //artist should be ABBA
// console.log(one_one[5439].artist);
console.log(2);
var two = updateRecords(collection, 5439, "tracks", "Take a Chance on Me") //tracks should have Take a Chance on Me as the last element.
var twoTrack = two[5439].tracks;
console.log(typeof twoTrack); //this should return array but instead it returns undefined
// console.log(twoTrack[twoTrack.length -1]);
// console.log(3)
// var three = updateRecords(collection, 2548, "artist", "") //artist should not be set
// console.log(three[2548].artist)
//Test Passed
// console.log(4)
// updateRecords(collection, 1245, "tracks", "Addicted to Love") //tracks should have Addicted to Love as the last element.
//Test Passed
// console.log(5)
// updateRecords(collection, 2468, "tracks", "Free") //tracks should have 1999 as the first element.
// console.log(6)
// updateRecords(collection, 2548, "tracks", "") //tracks should not be set
//Test Passed
// console.log(7)
// updateRecords(collection, 1245, "albumTitle", "Riptide") //albumTitle should be Riptide
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36
.
Challenge: Record Collection
Link to the challenge: