Question

In: Computer Science

In javascript, Write a compareTo method for the Playlist class. Playlists with fewer songs on them...

In javascript, Write a compareTo method for the Playlist class. Playlists with fewer songs on them come first. Playlists with the same number of songs come in alphabetical order by playlist name. Playlists with the same number of songs and same name can be considered equivalent (though this obviously isn’t exactly true).

Solutions

Expert Solution


// PlayList class
class PlayList {
        constructor(nsongs, name) {
                this.nsongs = nsongs;
                this.name = name;
        }
}
// compares number of songs if numbers are same then playlist names
function compareTo(a, b) {
        if (a.nsongs > b.nsongs) return 1;
        if (b.nsongs > a.nsongs) return -1;
        return a.name.localeCompare(b.name);
}

// creating array and pushing elements
var list = [];
list.push( new PlayList(2,"Vammie"));
list.push( new PlayList(1,"Gimmie"));
list.push( new PlayList(2,"Samie"));
list.push( new PlayList(5,"Lamie"));

// Sort array based on compareTo function
list.sort(compareTo);
        

Output:


Related Solutions

in javascript, Write a method for calculating (and returning) the total length of all songs in...
in javascript, Write a method for calculating (and returning) the total length of all songs in the playlist. Do not use indexes or a for-each loop (those would be perfectly fine ways to solve the problem, but we want to practice another way).
A playlist contains 15 songs, 5 songs from Artist 1, 5 songs from Artist 2, and...
A playlist contains 15 songs, 5 songs from Artist 1, 5 songs from Artist 2, and 5 songs from Artist 3. Shuffle the playlist (randomly) and listen to the first 6 songs on the playlist. Let Y1 be the number of songs from Artist 1 in the first 6 songs played and let Y2 be the number of songs from Artist 2 in the first 6 songs played. (a) Find the joint probability distribution function for Y1 and Y2. (b)...
Someone is listening to a playlist of 100 songs on an original iPod, and it is...
Someone is listening to a playlist of 100 songs on an original iPod, and it is programmed to play in random order. Sometimes the same song would play twice in a row; many people thought the iPod was broken. What was the problem?
iTunes library of mp3s has a very large number of songs. A student creates a playlist...
iTunes library of mp3s has a very large number of songs. A student creates a playlist of 45 randomly selected songs. The sample mean length of the songs is 241.27 seconds and the standard deviation is 93.40 seconds. Note: Numbers are randomized in each instance of this question. Pay attention to the numbers in this question. What is the lower bound for a 95% confidence interval for the actual mean length of the songs? Give your answer to 4 decimal...
A)You have 12 songs on your ipod. In how many ways can you make a playlist...
A)You have 12 songs on your ipod. In how many ways can you make a playlist of 5 songs for your workout? Yes order matter; playing fight songs before thinder is totally different from thunder before fight song. B) the model if car you are thinking of buying is available in none different colors and three different styles(hatchback,sedan, or station wagon). In how many ways can you order the car?
Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides...
Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides for lecture 8). Add the following methods to the class and submit the completed LinkedList class. int size() which returns the size of the linked list Link getElementByIndex(int index) which returns the Link/node specified by the index. For example, getElementByIndex(0) should return the first Link, getElementByIndex(2) should return the third Link. If index is not in range, your method should return null. boolean hasDuplicate()...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method,...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class. The exercises, for those without the text: 19. Modify the Point class from Chapter 8 so that it defines...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method,...
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class. The exercises, for those without the text: 19. Modify the Point class from Chapter 8 so that it defines...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts. As a reminder, you can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT