| Level Navigation: 1 | (2ℹ️) | 3 | 4 | 5 | 6 | (7ℹ️) | (8ℹ️) | (9ℹ️) | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | (37ℹ️) | 38⚡ | 39⚡ | 40⚡ | 41⚡ | 42 | 43⚡ | 44⚡ | 45 | 46 | (47ℹ️) |
Now let’s fix it by adding a replaceAll() call for exclamation marks:
export function toSnakeCase(text) {
return text.replaceAll(' ', '_').replaceAll('!', '_').toLowerCase();
}
What does this do?
replaceAll(' ', '_') - replaces all spaces with underscoresreplaceAll('!', '_') - replaces all exclamation marks with underscorestoLowerCase() - converts everything to lowercaseOops! We broke our old test!
When we add this feature to convert ! to _, we’re changing how our function works. Remember that test we wrote back in a previous level? It’s going to fail now!
In a previous level, we had a test that expected:
it('should handle text with special characters', () => {
const result = toSnakeCase('Hello World!');
expect(result).toBe('hello_world!'); // This expects the ! to be preserved
});
But now our function converts ! to _, so the result will be 'hello_world_' instead of 'hello_world!'.
Sometimes we need to change our tests! When we intentionally change how a function works, we need to update our tests to match the new behavior. Go back to that previous test and update it to expect 'hello_world_' instead of 'hello_world!'.
Try it now:
toSnakeCase function with the new feature'hello_world_' instead of 'hello_world!' or just remove it.