Record Collection - JavaScript Algorithms and Data Structures | FreeCodeCamp Solution

  Рет қаралды 1,519

Trending Youth

Trending Youth

Күн бұрын

#freeCodeCamp #JavaScriptDSA
📌 Link : www.freecodeca...
🚨Problem Statement:
You are given an object literal representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.
You start with an updateRecords function that takes an object literal, records, containing the musical album collection, an id, a prop (like artist or tracks), and a value. Complete the function using the rules below to modify the object passed to the function.
Your function must always return the entire record collection object.
If prop isn't tracks and value isn't an empty string, update or set that album's prop to value.
If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.
If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.
If value is an empty string, delete the given prop property from the album.
Note: A copy of the recordCollection object is used for the tests.
📢Solution :
// Setup
var 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.length != 0){
records[id][prop] = value;
}
else if(prop === "tracks" && !records[id].hasOwnProperty(prop)){
records[id][prop] = [];
records[id][prop].push(value);
}
else if(prop === "tracks" && value.length != 0){
records[id][prop].push(value);
}
else if(value.length === 0){
delete records[id][prop];
}
return records;
}

Пікірлер: 13
@coderdyslexic
@coderdyslexic 2 жыл бұрын
This solution works trust it... And thank you!! I was stuck on this one.
@TrendingYouth
@TrendingYouth 2 жыл бұрын
Great ☺
@andyackon2743
@andyackon2743 2 жыл бұрын
am still stuck on it, i can put in what am seeing here tho but i want to really understand it
@xsui7180
@xsui7180 2 жыл бұрын
@@andyackon2743 I suggest you to skip it for now, since its a huge jump.
@suhassurse9669
@suhassurse9669 3 жыл бұрын
Awesome
@juniorrokudevelopertutoria3734
@juniorrokudevelopertutoria3734 2 жыл бұрын
This solution works
@TrendingYouth
@TrendingYouth 2 жыл бұрын
Yes it does ☺
@dandy90
@dandy90 2 жыл бұрын
I don't get where 'tracks' comes from on this line records[id][prop] = []; records[id][prop].push(value); I'm confused as hell
@TrendingYouth
@TrendingYouth 2 жыл бұрын
Problem statement : If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it. solution : If prop is equal to "tracks" and that record album does not have any property, then first we create an empty array by writing records[id][prop] = [], thereafter we push value inside that empty array by writing records[id][prop].push(value).
@theultimategamer7202
@theultimategamer7202 2 жыл бұрын
why record[id][prop] is used? is [prop] and ["prop"] ARE DIFFERENT WHILE ACCESING?
@TrendingYouth
@TrendingYouth 2 жыл бұрын
Yes, both are different. Considering [prop], prop is a variable with some value for eg: prop = "value". Whereas ["prop"] is when you directly pass value instead of variable. Let's say, prop = "abc" Then it can be either accessed as [prop] or it can also be accessed as ["abc"]. Both are ultimately the same thing.
@schawnnarajiri5843
@schawnnarajiri5843 3 жыл бұрын
you saved me
@TrendingYouth
@TrendingYouth 2 жыл бұрын
My pleasure 😊
Record Collection - Free Code Camp
15:42
Useful Programmer
Рет қаралды 20 М.
5 JavaScript Concepts You HAVE TO KNOW
9:38
James Q Quick
Рет қаралды 1,4 МЛН
这三姐弟太会藏了!#小丑#天使#路飞#家庭#搞笑
00:24
家庭搞笑日记
Рет қаралды 123 МЛН
SCHOOLBOY. Мама флексит 🫣👩🏻
00:41
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 7 МЛН
МАИНКРАФТ В РЕАЛЬНОЙ ЖИЗНИ!🌍
00:31
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 35 МЛН
Hashing Algorithms and Security - Computerphile
8:12
Computerphile
Рет қаралды 1,5 МЛН
Use these instead of vh
6:06
Kevin Powell
Рет қаралды 507 М.
Record Collection (Basic JavaScript) freeCodeCamp tutorial
4:44
Coding David
Рет қаралды 13 М.
freeCodeCamp Record Collection Challenge JavaScript
5:48
teachToLearn
Рет қаралды 15 М.
This is Why Programming Is Hard For you
10:48
The Coding Sloth
Рет қаралды 855 М.
NEW Tesla Prototype LEAKED at WB Studios | This Design Is Weird
20:34
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 408 М.
How I Would Learn Python FAST in 2024 (if I could start over)
12:19
Thu Vu data analytics
Рет қаралды 307 М.
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,2 МЛН
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 402 М.
这三姐弟太会藏了!#小丑#天使#路飞#家庭#搞笑
00:24
家庭搞笑日记
Рет қаралды 123 МЛН