You must implement a new class on JavaScript called "MyString" that computes a histogram representing the number of times each letter of the alphabet is in a given text. The class "MyString" must contain the following elements:
A property called "str" that stores the text.
A property called "letters", which is an object that stores key-value pairs, one for each letter of the text. The keys correspond to the letters and the values, to the number of times the letters appear in the text.
A constructor method that initialises "str" with a text introduced by the user when an instance of the class "MyString" is created. If the user does not provide any text, "str" should be initialised with the text "Hello, World!". The property "letter" should be initialised as an empty object.
A method called "frequency" that calculates the frequency each letter appears in "str and assign the output to "letters. The method should count lower and uppercase as the same letter. Punctuation as well as any other special character must be ignored.
A method called "histogram" that displays for each letter (including letters not in "str") a row of asterisks representing how many times the character appeared.
Table 1 summarises the tests that your implementation should pass in order to be considered it is complete and fully functional. Figure 1 shows the expected output for the "histogram" method.
Table 1: Tests on "MyString" class Test Example
Test
Example
Comments
Create new MyString object
let txt1 = new MyString
Initialize "str" with default string "Hello, World"
Create new MyString object passing in a String
let txt1 = new MyString("How many roads must walk down before you call a man? Yes, 'n' how many seas must a white dove sail before she sleeps in the sand? Yes, 'n' how many times must the cannonballs fly before they're forever banned? The answer, my friend, is blowin' in the wind, the answer is blowin' in the wind.")
Initialize the "str" with the passed value.
Call "frequency" method
txt1.frequency()
After the call, the property method "letters" must contain the letter frequencies as numerical values (e.g. {h: 13, o: 14, w: 12, m: 11, a: 19, ...) Call "histogram" txt1.histogram)
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.