Hello, can someone please explain - in terms that would be used by a non-tech manager, for example - what the goal of the code should be? So please don’t use any coding terms like array or push or function.
I know that we have a list of albums with data about each album (and some missing data) and that we want to manipulate that data in some way to achieve some end result. But I am getting all knotted up in the tech “trees” of this challenge that I cannot see the “forest”.
And I don’t want to see anyone’s else’s code b/c that will not allow me to independently think this through. I’ll only be reverse engineering from those answers.
The goal is specified by the challenge instructions. Why don’t you tell us which parts of the instructions are confusing to you? That way, we won’t waste time writing explanations for things you already understand.
Actual instructions: Write a function which takes an album’s id (like 2548), a property prop (like “artist” or “tracks”), and a value (like “Addicted to Love”) to modify the data in this collection.
How I interpret that:
function function_name(id, prop, value){
***code that does stuff***
}
Actual instructions: If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.
How I interpret that:
Find all the non-empty properties that are not “tracks” and…this is where I get confused. So I will end up with a collection of these properties/values:
2548: an object,
album: slippery when wet,
artist: bon jovi,
tracks: let it rock, you give love a bad name
But what do I do with them? The instructions [update or set the value for that record album’s property] seem to suggest I should set these values for those properties…but those values already exist for those properties…??
If the prop argument passed into the function is not “tracks” and the value argument passed into the function is not a blank string, you will need to update or set a value for that album’s property. When you read further down into the instructions, you get more information about what to do if val is blank or not. At this point in the instructions, you just need to understand you will need to do something if those two conditions are true.
OK I looked up the final answer b/c I was just hopelessly lost.
And based on that and the instructions:
we are given 3 values (id, prop, value).
For the given id:
1. if prop <> tracks & value <> "": set that collection.id.prop = value.
2. If prop === tracks but collection.id.tracks does not exist: create collection.id.tracks array and set it to value.
3. If prop === tracks and value <> "": add value to tracks.
These steps can be streamlined:
IF value <> "":
AND IF prop === tracks THEN: add value to tracks
ELSE: set that collection.id.prop to = value
ELSE:
IF prop === tracks (do I also need to check if collection.id.tracks exists at this point in the IF/THEN code?):
create collection.id.tracks array and set it to value
If prop === tracks but collection[id].tracks does not exist: create collection.id.tracks array and add value to the array.
Note above I used bracket notation instead of dot notation for id. Why? Because there is no property named id in collection. id is a variable, so to reference a property with a variable you must use bracket notation.
Your streamlined version would be more accurate and concise if written as:
IF value <> "":
AND IF prop === tracks THEN:
IF tracks property exists add value to tracks
ELSE: create tracks array and add value to the array
ELSE: set collection[id].prop to = value
ELSE:
Delete prop from the collection