Switch Statement instead of if/else if

Can I use a switch statement instead of using If/else if statement to implement this code ?

// 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) {
return records;
}

updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);





      **Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36</code>

**Challenge:**  Record Collection

**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

I can see how it would be done, but it would be challenging. A switch requires that the switch be equal to a particular case. So you could switch on prop, maybe, but it’ll be either track or anything else. Or you could switch on value, maybe - but it’ll either be an empty string or *anything" else.

So either way, you’ll have a switch with one case and a default catchall. At that point, what’s the advantage of a switch?

1 Like

You can use whatever you can program. So, can you write a switch-case to do this? Only one way to find out: Try to use switch-case to do this :wink:

Trial-and-Error is not only a valid strategy to learn, it’s going to give you more insight into decision making than just to copy whatever we might be telling you.
So I’d say go ahead and try.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.