Question: JavaScript -- Please Rework This Code So it Still Produces Same Outcome const songs = [ { title: 'Willow', artist: 'Taylor Swift', position: 19, weeksOnChart:

JavaScript -- Please Rework This Code So it Still Produces Same Outcome const songs = [ { title: 'Willow', artist: 'Taylor Swift', position: 19, weeksOnChart: 7 }, { title: 'Sweet Melody', artist: 'Little Mix', position: 6, weeksOnChart: 14 }, { title: 'Paradise', artist: 'Meduza Ft Dermot Kennedy', position: 5, weeksOnChart: 13 }, { title: 'Anyone', artist: 'Justin Bieber', position: 7, weeksOnChart: 4 }, { title: 'Train Wreck', artist: 'James Arthur', position: 24, weeksOnChart: 13 }, ]; const catalogObject = { _songs: [], _maxTitleLength: "Title".length, _maxArtistLength: "Artist".length, _maxPositionLength: "Position".length, _maxWeeksOnChartLength: "Weeks On Chart".length, addSong(props) { const song = {}; let dataFound = false; if (props.title) { const title = props.title.trim(); song.title = title; dataFound = true; if (title.length > this._maxTitleLength) { this._maxTitleLength = title.length; } } if (props.artist) { const artist = props.artist.trim(); song.artist = artist; dataFound = true; if (artist.length > this._maxArtistLength) { this._maxArtistLength = artist.length; } } if (props.position) { song.position = props.position; dataFound = true; if (String(song.position).length > this._maxPositionLength) { this._maxPositionLength = String(song.position).length; } } if (props.weeksOnChart) { song.weeksOnChart = props.weeksOnChart; dataFound = true; if (String(song.weeksOnChart).length > this._maxWeeksOnChartLength) { this._maxWeeksOnChartLength = String(song.weeksOnChart).length; } } if (dataFound) { this._songs.push(song); } }, getSongByIndex(index) { let song = {}; if (index >= 0 && index < this._songs.length) { if (this._songs[index]) { song = this._songs[index]; } } return song; }, getSongByTitle(title) { let song = {}; for (let index = 0; index < this._songs.length; index++) { if (this._songs[index].title == title) { song = this._songs[index]; break; } } return song; }, listSongs(sorted = false) { const songs = !sorted ? this._songs : this._songs.sort(function (a,b) { if(a.position > b.position) { return 1; } else if(a.position < b.position) { return -1; } else { return 0; } }); if (songs.length > 0) { const position = this.padString("Position", this._maxPositionLength, ' ', false); const title = this.padString("Title", this._maxTitleLength, ' ', false); const artist = this.padString("Artist", this._maxArtistLength, ' ', false); const weeksOnChart = "Weeks On Chart"; console.log(`${position} ${title} ${artist} ${weeksOnChart}`) for (let index = 0; index < songs.length; index++) { const song = songs[index]; const position = this.padString(song.position, this._maxPositionLength, ' ', false); const title = this.padString(song.title, this._maxTitleLength, ' ', false); const artist = this.padString(song.artist, this._maxArtistLength, ' ', false); //console.log(song.position + ' - ' + song.title + ', ' + song.artist + ' (' + song.weeksOnChart + ')'); // Template literal console.log(`${position} ${title} ${artist} ${weeksOnChart}`) } } }, padString(data, maxLength, padCharacter, padLeft) { const paddingCount = maxLength - String(data).length; const padding = paddingCount > 0 ? Array(paddingCount + 1).join(padCharacter) : ''; if (padLeft) { return padding + String(data); } else { return String(data) + padding; } } } const catalog = Object.create(catalogObject); for (let index = 0; index < songs.length; index++) { catalog.addSong(songs[index]); } catalog.listSongs(); catalog.listSongs(true); console.log(catalog.getSongByTitle("Sweet Melody")); console.log(catalog.getSongByIndex(2));

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!