Skip to curriculum
Jump to the editor

2.2. String.prototype.repeat

Easy
Topics

This question is in

2. Strings and text
10–20 min

"-".repeat(20) draws a divider, " ".repeat(depth) indents a tree, and "*".repeat(stars) draws a rating. Rebuilding repeat yourself is a short exercise in how a built-in method really works: how it converts its argument, when it throws, what this is inside a method on String.prototype, and how to build a long string without a long loop.

Write String.prototype.myRepeat(count) in repeat.js. The starter already adds it to String.prototype with Object.defineProperty; fill in the function.

Signature

JAVASCRIPT
"text".myRepeat(count) // a string
  • this is the string the method is called on.
  • count is how many copies to join.

Requirements

  1. Returns the string repeated count times: "ab".myRepeat(3) is "ababab". A count of 1 gives the string, 0 gives "", and an empty string stays empty for any finite count.
  2. Works for large counts (a million copies) within the time limit, and for an empty string with a count of a billion.
  3. Converts count to a number ("3", true, an object with valueOf) and drops its fraction toward zero: 2.9 means 2. A BigInt or a Symbol count (or an object whose valueOf returns one) throws a TypeError, as the built-in does.
  4. A missing count, NaN, or a string that is not a number counts as 0. A count between -1 and 0 (such as -0.5) becomes 0, not an error.
  5. Throws a RangeError for a negative count (after dropping the fraction) and for Infinity, even on an empty string.
  6. Converts this to a string, so it works when called on a number, a BigInt, a boolean or a String object, and always returns a primitive string. Called on a Symbol, it throws a TypeError, as the built-in does.
  7. Throws a TypeError when called on null or undefined, before it looks at the count.
  8. Uses none of String.prototype.repeat, padStart, padEnd or Array.prototype.join, and adds no enumerable property to strings.

Examples

JAVASCRIPT
"ab".myRepeat(3);      // "ababab"
"ab".myRepeat(2.9);    // "abab"
"ab".myRepeat("2");    // "abab"
"ab".myRepeat();       // ""
"ab".myRepeat(-0.5);   // ""
JAVASCRIPT
"ab".myRepeat(-1);       // throws RangeError
"".myRepeat(Infinity);   // throws RangeError
String.prototype.myRepeat.call(12, 2);    // "1212"
String.prototype.myRepeat.call(null, 2);  // throws TypeError

Environment

  • Plain JavaScript in a browser page; the tests call myRepeat on strings and with String.prototype.myRepeat.call(value, count).
  • Not allowed: String.prototype.repeat, padStart, padEnd, Array.prototype.join. The tests replace them with functions that throw.
  • Counts so large that the result would pass the engine's maximum string length are not part of the tests.

Hints

  1. In a function that is not in strict mode, what is this when you call it with .call(null)?
  2. To make 8 copies, how many additions do you need if each step can double what you have?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…