Albert Jovinskyi
Albert Jovinskyi

Reputation: 349

How to assign a variable which function takes as a parameter

I'm trying to do something like this, but variable is not Assign (

const insert = (str, index, pasteString) => {
  let res;
  if (index > 0) {
    res = str.substring(0, index) + pasteString + str.substring(index, str.length);
  } else {
    res = pasteString + str;
  }
  str = res;
}

Here's i'm trying to call this function

const filterDescription = (obj) => {
  const str = obj.description;
  const strLen = str.length;
  const fifty = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 2));
  const oneOfFour = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 4));
  const oneOfThree = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 3));

  console.log([fifty, oneOfFour, oneOfThree]);
  insert(str, fifty, `string`);
  insert(str, oneOfFour, `string`);
  insert(str, oneOfThree, `string`);
  insert(str, 1, `string`);
  insert(str, strLen - 1, `string`);
  return str;
}

Upvotes: 1

Views: 53

Answers (2)

Legolas
Legolas

Reputation: 773

You can pass the variable pointer or betterly said, pass the str variable by reference to the function and the update it.

Upvotes: 0

Lucas
Lucas

Reputation: 36

Maybe you forgot to set a return on the final of the function:

const insert = (str, index, pasteString) => {
    let res;
    if (index > 0) {
        res = str.substring(0, index) + pasteString + str.substring(index, str.length);
    } else {
        res = pasteString + str;
    }
    return res;
}

Did you try to do this?

Upvotes: 2

Related Questions