I'm trying to replace the "..." in a given string with other different strings stored into an array using this function:
`
function addTokens(input, tokens)
{
let returnInput = input;
if ((typeof input === 'string') || (input instanceof String))
{
if(input.length >= 6)
{
for (let i = 0; i < tokens.length; i++)
{
if((tokens[i].tokenName === 'string') || (tokens[i].tokenName instanceof String))
{
returnInput = returnInput.replace("...",`${tokens[i].tokenName}`);
}
else
{
//throw new Error("Invalid array format");
}
}
}
else
{
//throw new Error("Input should have at least 6 characters");
}
}
else
{
//throw new Error("Input should be a string");
}
return returnInput;
}
`
I have even commented the errors so that they don't interfere in any way, but it doesn't seem to work; if I call
result = addTokens('User ...', [{ tokenName: 'user' }])
console.log(result)
, it just returns the initial "User ..." instead of the "User ${user}" I want it to.
However, when I run this little bit of code:
`
let str = "User ...";
let tok = [{ tokenName: "user" }]
str = str.replace("...", `${tok[0].tokenName}`);
console.log(str);
`
by itself, it does exactly what I intend it to do.
Can anyone help me find out what the issue is? Sorry for any rookie mistakes, I'm new to Javascript. Thanks!