Trouble with .replaceAll() Method

The goal is to take all of the spaces out of a string with a single line using the .replaceAll() method. It appears looping .replade90 methods will s work because this works welsewhere in the function:
f = f.replace(s.charAt(i), ''); // returning f without the specified character from s

This does not work, however:
s = s.replaceAll(' ', '');
The error returned is “TypeError: s.replaceAll is not a function”

It isn’t a function, according to MDN Web Docs, it’s a method, just like .replace().
What might I be doing wrong?



What programming language are you using? I assume it’s JavaScript. And where are you trying to execute this code? NodeJS, browser?

Try printing s before the line s = s.replaceAll(' ', '');. Maybe it’s not a string, but undefined or something else.

Does this?

s = s.replaceAll(/\s/g, '');

As it turns out, yes…

 > s = 'a quick brown fox jumps over the lazy dog'
<- 'a quick brown fox jumps over the lazy dog'
 > s.replaceAll(/\s/g, '')
<- 'aquickbrownfoxjumpsoverthelazydog'

But on closer review, so does your code…

 > s.replaceAll(' ', '')
<- 'aquickbrownfoxjumpsoverthelazydog'
 > t = s.replaceAll(' ', '')
<- 'aquickbrownfoxjumpsoverthelazydog'
 > t
<- 'aquickbrownfoxjumpsoverthelazydog'

Given this is not the issue, one might need to examine other areas more closely.

Thanks, Roy and Aleksandr for the replies.

Yes, Javascript. And yes, the method works elsewhere. And, yes, the s variable is a string. I’m writing/testing in the Codecademy JS Practice space. Here are the function and the error:

function calculateDif(start, finish) {
let d = 0;
// remove spaces from start and finish
let s = start.trim();
let f = finish.trim();
console.log(s, f);
// then internal spaces
s = s.replaceAll(’ ‘, ‘’); // <==
f = f.replaceAll(’ ', ‘’);
console.log(s, f);
// initially set dif to abs difference in length
d = Math.abs(s.length - f.length);
// eliminate common letters
for (i=0; i < s.length; i++) {
if (f.includes(s.charAt(i))) {
f = f.replace(s.charAt(i), ‘’);
console.log(s, f);
d = d + f.length
return d;
console.log(calculatePar(‘blue bird’, ‘eagle’));
// expected output 3

ERROR text:
s = s.replaceAll(’ ', ‘’);

TypeError: s.replaceAll is not a function
at calculateDif (/home/ccuser/workspace/sandbox-node-14-61980271e82514494134c54e/script.js:34:8)
at Object. (/home/ccuser/workspace/sandbox-node-14-61980271e82514494134c54e/script.js:49:13)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47

This method may not be supported by the version of JS (or Node.js) behind the LE. There are two alternative approaches using string.replace():

  • iterative approach
while (true) {
  if (s.indexOf(' ') < 0) {
  s = s.replace(' ', '')
  • greedy regex
s.replace(/\s/g, '')

Thanks, Roy. Yeah I’d already written a loop, but want to be more elegant.


At least now you have a workaround when .replaceAll() isn’t supported. We can see why the method was introduced.

Notice there is no change in the arguments when a regex is used? The only requirement is that in .replaceAll() there must be a global flag, which is not the case with .replace() though can be used with equal effectiveness. The winner in this addition is the substr since All is explicitly greedy.

There’s always a work around. Your speculation that replaceeAll() may not be supported by the Node.js used by Codecademy led me to check, and that’s correct. Codecademy is using v14… ReplaceAll() requires v15… or later.

1 Like