Replace method error - " not a function" --replacing string with new string

Hi i am checking to see if str2 characters are in str1 characters. If there are duplicates it causes problems.

My idea was to remove the character(currentValue) in str1 once it has been used once. I chose the replace method for this. However ,it is not working. May i know why?

is it because currentValue can only be called within the map function?


   split1.replace(currentValue, "");
function scramble(str1, str2) {
  //1) create a variable that stores the word
  let stored = "";
  let bool = false;
  //2) split str1 to an array
  const split1 = str1.split("");
  //2 split str 2 into ther own strings values
  repeated = 0;
  let split2splice = split2;
  //3) map over the array for each str2 value, index, arr) {
    if (split1.includes(currentValue)) {
      stored += currentValue;
      bool = true;
      split1.replace(currentValue, "");
    } else {
      bool = false;
      stored = "";

  //4) if it has, add it to stored4
  //5) if at any point there isnt a letter, return false
  // 5a) if there are two same values in str2 and only 1 same value in str1, it means it will still return false.
  //need to find a way to return false if there are two
  //you have to delete the character in str1 once its been used once
  //6) once str1 has returned the value you need to alter str1 and delete the value that was just passed
  //7) if the map is all ok, return true to stored

  if (stored.length === str2.length) {
    bool = true;
  } else {
    bool = false;
  return bool;

scramble("comaehisa", "commas");

map returns a new array. why don’t you use forEach if you don’t intend to use that feature?
This is a misuse of methods - it’s a bad habit, try to not keep it!

anyway you get the error that split1.replace is not a function because you are trying to use replace on an array, when it is a string method.
Array.prototype.replace doesn’t exist, so you get that error. The one that does exist is String.prototype.replace

you may also get that is not a function, if it is so is because you can’t use array methods on undefined values

What’s the goal here?

oh thats wrong its supposed to be const split2 = str2.split("");

so split2 is an array? then first part of my last post still apply.

hey thanks for that input in regards to ForEach.

and noted in regards to the replace method. Didn’t realise that.:smiley: