Tell us what’s happening:
I am pretty stuck! And don’t know where to start? I already read 10 times what they want. Also, I look back at how Accessing Objects Properties with Variables and MANUPULATING COMPLEX OBJECTS goes. And it’s pretty clear they also want a Array + if else statement. And if the object isn’t there use te push element etc.
But I have zero clue how to start off? I don’t need to know the answer (because you not gonna learn from knowing the answer) But can someone explain to me the process?
Your code so far
// Setup
var 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
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection/
Think about all the cases you need to check with the if else clauses.
For example, if value is an empty string or not, if the object has the property or not, if the property is tracks or not, etc. And how those combine with each other to cover what is needed.
1 Like
Try writing out an algorithm with all the steps you think it will take to accomplish the task. Write it in plain language or pseudo-code and using your steps, walk through the test cases to see if your written logic would work to pass the tests. Once you have that written, then you can think about the code needed to fulfill your algorithm.
1 Like
Tell us what’s happening:
For the people who already helped me with Record collection thanks! randelldawson It really helped to think step by step the task I need to accomplise.
Anyway, i’m still stuck… can someone help me furter including a explanation why I should add that etc.
Your code so far
// Setup
var 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
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
var array = new array [];
if(prop != "tacks" && value != "") {
collection[prop] = value;
}
if(prop == "tracks" && "track" == ""){
// how to add an empty array?
}
if(prop == "tracks" && value != ""){
//push the code
}
if(value == ""){
delete collection[prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection
Please do not create multiple topics for the same issue. I have merged your topics.
1 Like
thanks! I was looking up how to merged but couldn’t find it.
Why do you check if track
is an empty string?
There says tacks
instead of tracks
.
Hi zdflower!
What I read was:
If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.
So, if prop is “track” (that’s why the equel sign == )
but, “album” doesn’t have “track” (probably should do: “album” != “track”)
First organize the cases, i.e) find the minimum amount of cases that you must handle.
Unorganized cases looks like this:
-
prop
is “tracks” and collection
doesn’t have that prop
-
prop
is “tracks” and collection
has that prop
- prop is “tracks” and
value
exists
-
value
is empty
The problem with above is, it checks for too many duplicated subcases.
- Look at #1 and #2, If you’ve checked collection doesn’t have “trakcs”
prop
, you don’t have to check for collection has prop, “track”.
- Look at #3 and #4. If you’ve checked
value
exists, then you don’t have to check for value
does not exist.
Here’s the organized cases:
-
value
doesn’t exist
-
prop
is “tracks”
- collection doesn’t have prop, “tracks”.
Under each case, specify how to handle.
if value empty
delete prop
done
if prop is tracks
if collection has no tracks
make empty tracks
add value to tracks
done
I think you can code that logic.
1 Like
Ok tbajs a lor.
This should be m uch better, but I still have problem with adding value to a collection in case prop exists but value is empty.
// Setup
var 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
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (value="") {
delete collection[id].prop;
}
if (prop="tracks") {
if (collection[id].tracks= "") {
collection[id].tracks= [];
}
collection[id].push(value);
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
=
not a comparison operator.