Question: Language: JavaScript Working with this code: function padString(data, maxLength, padCharacter, padLeft) { while(data.length

Language: JavaScript

Working with this code:

function padString(data, maxLength, padCharacter, padLeft)

{

while(data.length

{

if(padLeft)

{

data=padCharacter+data;

}

else

{

data=data+padCharacter;

}

}

return data;

}

const songs = [

{

title: "Drivers License",

artist: "Olivia Rodrigo",

position: 1,

weeksOnChart: 3,

},

{

title: "Mood",

artist: "24kGoldn Featuring iann dior",

position: 2,

weeksOnChart: 25,

},

{

title: "Blinding Lights",

artist: "The Weekend",

position: 3,

weeksOnChart: 60,

},

{

title: "34+35",

artist: "Ariana Grande",

position: 4,

weeksOnChart: 13,

},

{

title: "Levitating",

artist: "Dua Lipa Featuring DaBaby",

position: 5,

weeksOnChart: 17,

}

]

#7

let catalogObject = {

_songs: [],

addSongs: function(props)

{

props.title = props.title.trim()

props.artists = props.artist.trim()

this._songs.push(props);

},

listSongs: function ()

{

for (let i = 0; i < this._songs.length; i++)

{

let ratio = this._songs[i];

console.log(`${ratio.position} - ${ratio.title}, ${ratio.artist} (${ratio.weeksOnChart})`);

}

}

}

const catalog = Object.create(catalogObject);

for (let i = 0; i < 5; i++)

{

catalog.addSongs(songs[i]);

}

catalog.listSongs();

----------------------------

Question:

Add padString() to catalogObject, and update addSong() and listSongs() to support padded output The output in the Console isn't very easy to read, so format the output so that each column is padded.

  • Move the padString() function into the catalogObject
  • Add the following properties to catalogObject to keep track of the maximum length of each column as a song is added to the catalog.
    • _maxTitleLength: Maximum title property length, initialized to the length of column header "Title"
    • _maxArtistLength: Maximum artist property length, initialized to the length of column header "Artist"
    • _maxPositionLength: Maximum position property length, initialized to the length of column header "Position"
    • _maxWeeksOnChartLength: Maximum artist property length, initialized to the length of column header "Weeks On Chart"
  • Update addSong() to update each of the maximum length properties as a song is added
  • Update listSongs() to use padString() and each of the maximum length properties to format the catalog Console output, and to include column headers, also padded

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!