this is the code I have written, and it is not working.
// Setup
const recordCollection = {
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(records, id, prop, value) {
if (prop!==recordCollection.tracks && value!==0) {
prop===value;
}
else if (prop===recordCollection.tracks && recordCollection.tracks===0) {
const tracks =[value];
}
else if (prop===recordCollection.tracks && value !==0) {
recordCollection.tracks.push(value);
}
else if (value===0) {
recordCollection.artist = 0;
recordCollection.tracks = 0;
}
else {
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Now to me, obviously it makes perfect sense and it needs to work, but it isnāt working because I have made mistakes.
I am stuck. I would love pointers to direct me in the right way.
I donāt want to look at the answer because I rather figure it out by myself, with some help.
Ty!
edit: A question I have, I added else to end the else if statement and I wrapped {} around the return statement after I do that I donāt have any completed assignments is it because the whole code is wrong, and is that why the one completed assignment that had been as the default is gone now?
Hi @TheOctagon3323 ,
It makes it easier for us to help if you include the link to the problem. otherwise we have to go searching for it.
As a matter of interest any problem you are struggling with you can request help for by pressing the Get Help button and selecting āask for helpā.
It creates a forum post complete with your code and any links or information that we might need to debug it.
Hey, So Iāve looked at your code and spotted most of the issues. Ill try and give you some hints.
Hint 1) One of the best ways to solve a problem like this is to make sure that you solve it systematically. You have broken down the problem into four sections but you should focus on making sure that each one is working as intended before moving on.
Hint 2) A good thing to do is to create variables and console.log() their values to make sure they are what you intended.
Hint 3) donāt give yourself unnecessary work prop!==recordCollection.tracks
has less room for error if insted you try: prop !== "tracks"
Hint 5) Your solution to the line below is quite far from the mark. try using the hasOwnProperty() method. you can research it here
" If prop is tracks but the album doesnāt have a tracks property, create an empty array and add value to it."
for hint number 3 : I tried using tracks but it says it is not defined how come I should put double quotes around it? is it because itās a property and it is automatically double quoted?
Iāve still got my old answer here. Iām afraid Iām off to bed but Ill leave you the solution I found to each of the sections. They are behind spoiler banners so you donāt have to look!
If prop isnāt tracks and value isnāt an empty string, update or set that albumās prop to value.
if (prop !== "tracks" && value !== ""){
console.log("chose 1");
records[id][prop] = value;
}
If prop is tracks but the album doesnāt have a tracks property, create an empty array and add value to it.
// Setup
const recordCollection = {
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(records, id, prop, value) {
if (prop!=="tracks" && value!==0) {
recordCollection.artist = 'value';
recordCollection.tracks = 'value';
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
trying to solve one condition by one now.
if there is a parameter value, and the parameter is a string do I need to type as value or as āvalueā? itās confusing, but I donāt think that is the issue as well.
edit: another question if you can answer please.
damianicely suggested this
Hint 3) donāt give yourself unnecessary work prop!==recordCollection.tracks
has less room for error if insted you try: prop !== "tracks"
If I want to use an object property inside my statements I have to put the strings?
I know JS does it automatically but I guess itās a different scope?
Yeah I remember, you can use either dot or bracket.
and I think I learned that you use bracket if you have a space in the property name is that correct?
You also need to use bracket notation when you have a variable holding the property name.
āHard codeā in this context means that you are writing the code to only work for one (or in this case two) property name instead of using the name stored in the variable prop.
// Setup
const recordCollection = {
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(records, id, prop, value) {
if (prop!=="tracks" && value!==0) {
records[prop] = records[id][prop];
console.log(records[prop]);
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
so it gives me undefined. I have a few questions.
you gave me a hint only to use the parameters. I thought value is any of the key values. I looked at the hint they hinted to use records[id][prop].
I can understand why it is undefined because those are just parameters, they have no connection to the object itself.
I can also understand this āAnother use of bracket notation on objects is to access a property which is stored as the value of a variableā
but parameters here are just parameters, they are nothing not a value not a variable.
itās very confusing to me any hints ;p?
the parameters get a value when the function is called, so you need to consider what possible values they can have, and make your function act accordingly depending on the input to get the wanted output
careful here, you are changing records and adding a new property with value undefined