A possible implementation is shown below:
1
2
3
4
5
6
7
8
9
|
String.prototype.repeatify = String.prototype.repeatify || function (times) { var str = '' ; for ( var i = 0; i < times; i++) { str += this ; } return str; }; |
The question tests the knowledge of the developer about inheritance in JavaScript and the prototype
property. It also verifies that the developer is able to extend native data type functionalities (although this should not be done).
Another important point here is to demonstrate that you are aware about how to not override possible already defined functions. This is done by testing that the function didn’t exist before defining your own: