To tackle this problem, I need to create an empty array and then add characters from the provided sentence into it, excluding asterisks. This approach will help me when I eventually need to iterate through the array items for replacements.
function replaceAsterisk(sentence, newWords) {
let newArray = [];
let character = sentence.split("");
console.log(character);
// If the character is not an asterisk, push it into the new array
if (character !== "*") {
newArray.push(character);
}
// If the character is an asterisk, push "cat" into the new array
else {
newArray.push("cat");
}
// Return the new array as a string
return newArray.join(" ");
}
console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));
Despite these instructions, the code still isn't adding "cat" to the array - what could be causing this issue?