JS – OOP in JavaScript

JavaScript is probably the language that I have decided to specialize during the first half of the 2016. 🙂 Thus, today I have taken some 3 of the homework tasks of the SoftUni in the Advanced JavaScript Course, just to check whether I can solve them. It took some time, but at the end the result was as expected. All of the problems are available here.

JS

Thus, I will start with the first problem – a simple class creation:

A fellow programmer tried to create a simple “class” in JavaScript but he’s got a problem getting it to work. Fix any bugs you may find in his code.

The code had to work also with “name”, which returns firstName + lastName. Quite trivial, but this was the first problem. This is my solution:

The second problem was a little more tricky. This is its original version:

After you’ve solved your friend’s problem, now he’s got another one. He wants to be able to change the names. Rewrite the function Person() so that it will work according to what his boss wants.

You should be able to get and update the values of the first and last name. If you change the first or last name, the full name will need to change automatically. Also, if you change the full name, the first and last names must change too. Refer to the samples to get a better understanding. Assume that all data will be valid (e. g., for person.fullName you will always get two words, separated by a single space).

Note that firstName, lastName and fullName are properties (not functions).

Hint: You may need to check one of the rather “exotic” ways to work with properties in JavaScript.

For the samples, assume the following variable exists:

var person = new Person(“Peter”, “Jackson”);

And this is how I have resolved it:

The get and the set option in JS are something I have to master further, but somehow I managed to do it there. This is the output of the code:

getset

The third problem I have managed to resolve reminded me of one article concerning JavaScript prototypes that I have written about an year ago. Anyway, I have read it and started to work again. This is the description of the problem:

Write the following methods which work on strings. All methods should return new strings, and should not modify the existing string instances.

  • startsWith(substring) – returns true if the string starts with the provided substring and false otherwise
  • endsWith(substring) – returns true if the string ends with the provided substring and false otherwise
  • left(count) – returns the first count characters of the string. If count is greater than the length of the string, returns the whole string
  • right(count) – returns the last count characters of the string. If count is greater than the length of the string, returns the whole string
  • padLeft(count, character) – returns a new string which contains count times the specified character at its beginning. character is optional and defaults to space
  • padRight(count, character) – returns a new string which contains count times the specified character at its end. character is optional and defaults to space
  • repeat(count) – repeats the provided string count times. Do not use the default implementation here, write your own

Try to make all methods as efficient as possible (they should consume little memory and take little time to complete). Assume that all parameters will be correct (e. g. substring will be a string, count will be a number, etc.).

Well, to be honest, this was challenging. Especially the idea of optional characters in padLeft/Right. But at the end I managed it 🙂

This is the solution with some tests:

So, pretty much that is it. Writing JavaScript is nice, if you have expected output. Thus, the homeworks of SoftUni can be challenging. The code in GitHub is available here.

That’s all folks! 🙂

Tagged with: ,