CodingNic

Practical Challenges

Reimplement the Builtins - Strings

Practical Challenges 45 min read

Reimplement the Builtins - Strings

Objectives

This chapter introduces no new concepts. Each challenge below asks you to rebuild a familiar built-in method yourself, using only what’s been taught so far.

Challenge 1: appendToString

Write a function appendToString(str1, str2) that returns a new string with str2 added onto the end of str1.

Example:

javascript
appendToString("Hello", " World!"); // "Hello World!"
appendToString("Foo", "bar");       // "Foobar"
appendToString("bar", "Foo");       // "barFoo"
appendToString("", "test");         // "test"
appendToString("other test", "");   // "other test"

Hint: template literals or the + operator both work here. Watch the order the two strings go in.

Challenge 2: prependToString

Write a function prependToString(str1, str2) that returns a new string with str2 added onto the front of str1.

Example:

javascript
prependToString("awesome", "very");   // "veryawesome"
prependToString("world", "hello ");   // "hello world"
prependToString("nothing", "");       // "nothing"

Hint: this is nearly the same problem as the last one, just with the two strings in the opposite order.

Challenge 3: charAt

Write a function charAt(str, index) that returns the character of str at position index. If index is greater than the string’s length, return an empty string.

Example: charAt("awesome", 2) returns "e". charAt("awesome", 12) returns "".

Constraint: don’t use the built-in .charAt() method.

Hint: you can read a single character out of a string with bracket notation, the same way you’d read an item out of an array by its index.

Challenge 4: stringIncludes

Write a function stringIncludes(str, char) that accepts a string and a single character, and returns true if the character appears anywhere in the string, false otherwise.

Example: stringIncludes("awesome", "e") returns true. stringIncludes("awesome", "z") returns false.

Constraint: don’t use the built-in .includes() method.

Hint: loop over the string one character at a time and compare each one against char. You can return early the moment you find a match.

Challenge 5: stringIndexOf

Write a function stringIndexOf(str, char) that accepts a string and a single character, and returns the first position at which that character appears, or -1 if it never appears.

Example: stringIndexOf("awesome", "e") returns 2. stringIndexOf("awesome", "z") returns -1.

Constraint: don’t use the built-in .indexOf() method.

Hint: a loop with an index variable gets you most of the way there, once you find a match you already know its position.

Challenge 6: stringLastIndexOf

Write a function stringLastIndexOf(str, char) that accepts a string and a single character, and returns the last position at which that character appears, or -1 if it never appears.

Example: stringLastIndexOf("awesome", "e") returns 6. stringLastIndexOf("awesome", "z") returns -1.

Constraint: don’t use the built-in .lastIndexOf() method.

Hint: this is close to the previous challenge, but instead of stopping at the first match, keep remembering the most recent one as you keep looping.

Challenge 7: repeat

Write a function repeat(str, n) that returns a new string consisting of str repeated n times back to back.

Example:

javascript
repeat("Jordan", 3); // "JordanJordanJordan"
repeat("Erin", 2);   // "ErinErin"
repeat("Maya", 0);   // ""

Constraint: don’t use the built-in .repeat() method.

Hint: start with an empty result string and build it up inside a loop that runs n times.

Challenge 8: removeFromString

Write a function removeFromString(str, start, count) that returns a new string with count characters removed from str, starting at index start. If count reaches past the end of the string, remove everything through the end.

Example:

javascript
removeFromString("Erin", 2, 2);          // "Er"
removeFromString("Erin", 0, 1);          // "rin"
removeFromString("Rithm School", 0, 6);  // "School"
removeFromString("Rithm School", 2, 4);  // "RiSchool"
removeFromString("Rithm School", 6, 400); // "Rithm "

Hint: .slice() was taught back in Module 5 and can grab the piece before the removed section and the piece after it separately. What happens if you ask .slice() for a section that runs past the end of the string?

Challenge 9: includes

Write a function includes(collection, value, fromIndex) that works on a string, an array, or an object. It should return true if value is found in collection, false otherwise. For a string or array, fromIndex is an optional starting position to search from. For an object, search among its values, an object has no order, so fromIndex is ignored.

Example:

javascript
includes([1, 2, 3], 1);           // true
includes([1, 2, 3], 1, 2);        // false
includes([1, 2, 3], 6);           // false
includes({ a: 1, b: 2 }, 1);      // true
includes({ a: 1, b: 2 }, "a");    // false
includes("abcd", "b");            // true
includes("abcd", "e");            // false
includes("abcd", "a", 2);         // false

Hint: the three collection types need three different search strategies. Figure out how to tell which kind of collection you’ve been handed before deciding how to search it, typeof and Array.isArray() (Module 5) can both help with that.

Recap

Nine methods you already rely on every day, rebuilt from scratch using nothing but loops, conditionals, and the small set of string and array tools this course covers. Rebuilding a built-in forces you to notice the edge cases it quietly handles for you.

Next lesson: reimplementing more built-ins, this time for arrays and objects.