Guest User

Untitled

a guest
Aug 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. // 71: String - `repeat()`
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3.  
  4. describe('`str.repeat(x)` appends `x` copies of `str` to each other and returns it', function() {
  5.  
  6. describe('pass the count to `str.repeat(count)`', function() {
  7. it('for `1` the string stays the same', function() {
  8. const what = 'one'.repeat(1); //Added the string 'one' and told it to repeat once
  9. assert.equal(what, 'one');
  10. });
  11. it('for `3` the string `x` becomes `xxx`', function() {
  12. const actual = 'x'.repeat(3); //Repeated the 'x' 3 times
  13. assert.equal(actual, 'xxx');
  14. });
  15. it('for `0` an empty string is returned', function() {
  16. const dontRepeat = 'zero'.repeat(0); //Repeated 0 times in order to return an empty string
  17. assert.equal('shrink'.repeat(dontRepeat), '');
  18. });
  19.  
  20. it('the count is not an int, such as "3", it gets coerced to an int', function() {
  21. const repeated = 'three'.repeat('3');
  22. assert.equal(repeated, 'threethreethree');
  23. });
  24. });
  25.  
  26. describe('throws an error for', function() {
  27. it('a count of <0', function() {
  28. const belowZero = -1; //Added a negative 1, so it would throw an error for a count < 0
  29. assert.throws(() => { ''.repeat(belowZero); }, RangeError);
  30. });
  31. it('a count of +Infinty', function() {
  32. let infinity = Infinity; //Added the Infinity JS keyword
  33. assert.throws(() => { ''.repeat(infinity); }, RangeError);
  34. });
  35. });
  36.  
  37. describe('accepts everything that can be coerced to a string', function() {
  38. it('e.g. a boolean', function() {
  39. let aBool = false; //Had to change the boolean from true to false
  40. assert.equal(String.prototype.repeat.call(aBool, 2), 'falsefalse');
  41. });
  42. it('e.g. a number', function() {
  43. let aNumber = 1; //Had to add a number 1
  44. assert.equal(String.prototype.repeat.call(aNumber, 2), '11');
  45. });
  46. });
  47.  
  48. describe('for my own (string) class', function() {
  49. it('calls `toString()` to make it a string', function() {
  50. class MyString { toString() { return 'my string'; } }
  51.  
  52. const expectedString = 'my string'; //Added my string
  53.  
  54. assert.equal(String(new MyString()).repeat(1), expectedString);
  55. });
  56. it('`toString()` is only called once', function() {
  57. let counter = 1;
  58. class X {
  59. toString() {
  60. return counter++;
  61. }
  62. }
  63.  
  64. let repeated = String(new X()).repeat(2);
  65. //what
  66.  
  67. assert.equal(repeated, '11');
  68. });
  69. });
  70.  
  71. });
Add Comment
Please, Sign In to add comment