Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 71: String - `repeat()`
- // To do: make all tests pass, leave the assert lines unchanged!
- describe('`str.repeat(x)` appends `x` copies of `str` to each other and returns it', function() {
- describe('pass the count to `str.repeat(count)`', function() {
- it('for `1` the string stays the same', function() {
- const what = 'one'.repeat(1); //Added the string 'one' and told it to repeat once
- assert.equal(what, 'one');
- });
- it('for `3` the string `x` becomes `xxx`', function() {
- const actual = 'x'.repeat(3); //Repeated the 'x' 3 times
- assert.equal(actual, 'xxx');
- });
- it('for `0` an empty string is returned', function() {
- const dontRepeat = 'zero'.repeat(0); //Repeated 0 times in order to return an empty string
- assert.equal('shrink'.repeat(dontRepeat), '');
- });
- it('the count is not an int, such as "3", it gets coerced to an int', function() {
- const repeated = 'three'.repeat('3');
- assert.equal(repeated, 'threethreethree');
- });
- });
- describe('throws an error for', function() {
- it('a count of <0', function() {
- const belowZero = -1; //Added a negative 1, so it would throw an error for a count < 0
- assert.throws(() => { ''.repeat(belowZero); }, RangeError);
- });
- it('a count of +Infinty', function() {
- let infinity = Infinity; //Added the Infinity JS keyword
- assert.throws(() => { ''.repeat(infinity); }, RangeError);
- });
- });
- describe('accepts everything that can be coerced to a string', function() {
- it('e.g. a boolean', function() {
- let aBool = false; //Had to change the boolean from true to false
- assert.equal(String.prototype.repeat.call(aBool, 2), 'falsefalse');
- });
- it('e.g. a number', function() {
- let aNumber = 1; //Had to add a number 1
- assert.equal(String.prototype.repeat.call(aNumber, 2), '11');
- });
- });
- describe('for my own (string) class', function() {
- it('calls `toString()` to make it a string', function() {
- class MyString { toString() { return 'my string'; } }
- const expectedString = 'my string'; //Added my string
- assert.equal(String(new MyString()).repeat(1), expectedString);
- });
- it('`toString()` is only called once', function() {
- let counter = 1;
- class X {
- toString() {
- return counter++;
- }
- }
- let repeated = String(new X()).repeat(2);
- //what
- assert.equal(repeated, '11');
- });
- });
- });
Add Comment
Please, Sign In to add comment