-2

Goal: Use an array method to remove the strings get, right, the, first, time and replace them with the single string know in secretMessage array.

Plan Define a function to text in the rain, the old string, and the new string and use to .replaceall() methods and pass each one of the different arguments through one at a time, reassigning the array each call.

Code Thus Far

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){=
  let updatedArray= orginalArray.replaceAll(oldString,updatedString);//Not Sure If I need to wrap this in []
  return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);

Current Results

let updatedArray= orginalArray.replaceAll(oldString,updatedString);
                               ^

TypeError:

orginalArray.replaceAll is not a function at replaceWords

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • I generally would just loop through the main array containing safe-words. Then check if the other array includes the selected safe-word (as it's looping). Then I would splice(index,1) and go from there. Reason for backwards is to not mess up index when we break it down on removals. – BGPHiJACK Dec 31 '21 at 13:34
  • 8
    `.replaceAll()` is a **string** method, not an array method. – Pointy Dec 31 '21 at 13:34
  • 2
    "*Define a function to text in the rain...*" - what? – David Thomas Dec 31 '21 at 13:38
  • 2
    Apart from what @Pointy already mentioned, please also pay attention to the difference between the strings `'time,'` and `'time'` – secan Dec 31 '21 at 13:48

5 Answers5

1

As stated by @Pointy's comment, replaceAll() is a string method, it's not available on an array.


So we'll need a way to apply something on each array index.


Luckily map() does exactly that:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){
  return orginalArray.map(item => {
    return item.replaceAll(oldString, updatedString)
  });
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);
0stone0
  • 34,288
  • 4
  • 39
  • 64
1

replaceAll is String function, not an Array function. If you want to replace a word that maybe exists as value in an array, you have to walk through that array. Your code would look like this:

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){
  /* because your array values are just single words 
  and you want to replace the whole word with another whole word, 
  you can use string comparison instead of replaceAll() */
  
  let updatedArray= orginalArray.map(string => (string == oldString ? updatedString : string));
  return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);
Apollo79
  • 674
  • 3
  • 14
1

Just for "fun", a solution using both an array method (Array.prototype.join()) and replaceAll() (String.prototype.replaceAll()) and close to your original idea, could be:

function replaceWords(arr, oldStr, newStr) {
  return arr.join(' ')          // join all the array items into a single space-separated string
    .replaceAll(oldStr, newStr) // call `replaceAll()` on the newly created string
    .split(' ');                // split the string back into an array
}

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];

secretMessage = replaceWords(secretMessage, 'get', 'know');
secretMessage = replaceWords(secretMessage, 'right', 'know');
secretMessage = replaceWords(secretMessage, 'the', 'know');
secretMessage = replaceWords(secretMessage, 'first', 'know');
secretMessage = replaceWords(secretMessage, 'time', 'know');

console.log(secretMessage);

Having said that, I think nobody would do it in that way in real life ;)

secan
  • 2,622
  • 1
  • 7
  • 24
-1

You can extend the array functionality to match your requirement. But not recommended.

Just add below code before your array declaration and your code should work.

Array.prototype.replaceAll = function(oldString, updatedString){
    return this.slice().map((str)=>((str === oldString)?updatedString:str));
}
Shiva
  • 728
  • 5
  • 13
-2

replaceAll() works for strings not arrays. Try the example below.

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){=
  let updatedArray= orginalArray.join().replaceAll(oldString,updatedString).split();
  return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);
wardialer
  • 490
  • 3
  • 7