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.
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.
1 2 3 4 5 6 7 8 9 |
function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; return {name: this.firstName + " " + this.lastName}; } |
The code had to work also with “name”, which returns firstName + lastName. Quite trivial, but this was the first problem. This is my solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<script> var Person = (function () { function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; this.name = firstName + " " + lastName; } return Person; }()); var peter = new Person("Peter", "Jackson"); var hr = "<hr>"; document.write(peter.name); document.write(hr); document.write(peter.firstName); document.write(hr); document.write(peter.lastName); </script> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<script> var Person = (function () { function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } return Person; }()); Object.defineProperty(Person.prototype, "fullName",{ get: function(){ return this.firstName + " " + this.lastName; }, set: function(value){ var fullName = value.split(" "); this.firstName = fullName[0]; this.lastName = fullName[1]; return this.firstName + " " + this.lastName; } }); var peter = new Person("Peter", "Jackson"); var hr = "<hr>"; document.write(peter.fullName); document.write(hr); document.write(peter.firstName); document.write(hr); document.write(peter.lastName); document.write(hr); peter.fullName = "Vitosh Academy"; document.write(peter.firstName); document.write(hr); document.write(peter.lastName); document.write(hr); peter.firstName = "Ale"; document.write(peter.fullName); </script> |
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<script> String.prototype.startsWith = function (substring) { var strSample = this.substring(0, substring.length); return strSample === substring; } String.prototype.endsWith = function (substring) { var strSample = this.substring(this.length - substring.length); return strSample === substring; } String.prototype.left = function (count) { return this.substring(0, count); } String.prototype.right = function (count) { return this.substring(this.length - count); } String.prototype.padLeft = function (count, character) { //character is optional if (typeof (character) === "undefined") { character = " "; } if (this.length <= count){ return Array(count + 1 - this.length).join(character) + this } else{ return this.toString(); } } String.prototype.padRight = function (count, character) { //character is optional if (typeof (character) === "undefined") { character = " "; } //ternary operator - we can do it in one line this way return this.length <= count ? this + Array(count + 1 - this.length).join(character) : this.toString(); } String.prototype.repeat = function (count) { return new Array(count ++).join(this).toString(); } var example = "This is an example string used only for demonstration purposes."; console.log(example.startsWith("This")); console.log(example.startsWith("this")); console.log(example.startsWith("other")); var example = "This is an example string used only for demonstration purposes."; console.log(example.endsWith("poses.")); console.log(example.endsWith ("example")); console.log(example.startsWith("something else")); var example = "This is an example string used only for demonstration purposes."; console.log(example.left(9)); console.log(example.left(90)); var example = "This is an example string used only for demonstration purposes."; console.log(example.right(9)); console.log(example.right(90)); // Combinations must also work var example = "abcdefgh"; console.log(example.left(5).right(2)); var hello = "hello"; console.log(hello.padLeft(5)); console.log(hello.padLeft(10)); console.log(hello.padLeft(5, ".")); console.log(hello.padLeft(10, ".")); console.log(hello.padLeft(2, ".")); var hello = "hello"; console.log(hello.padRight(5)); console.log(hello.padRight(10, "ale")); console.log(hello.padRight(5, ".")); console.log(hello.padRight(10, ".")); console.log(hello.padRight(2, ".")); var character = "*"; console.log(character.repeat(5)); // Alternative syntax console.log("~".repeat(3)); // Another combination console.log("*".repeat(5).padLeft(10, "-").padRight(15, "+")); </script> |
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! 🙂