I'm currently working on a JavaScript function that takes in two strings. The first string is any sentence provided by the user, and the second string consists of letters to be removed from the original sentence. My approach involves converting both strings into arrays and then using slice/splice methods to eliminate the specified characters.
let sentencePrompt = prompt("Please enter any sentence: ");
let removePrompt = prompt("Please enter a string of letter(s) to be removed from your original sentence: ");
function withoutString(sentence, str)
{
let sentenceArray = sentence.split("");
let removeArray = str.split("");
let sentenceLength = sentenceArray.length;
let removeLength = strArray.length - 1;
for(let i = 0; i < sentenceLength; i++)
{
if((sentenceArray.slice(i, i+removeLength)) === removeArray)
{
sentenceArray = sentenceArray.splice(i, i+removeLength);
}
}
return sentenceArray;
}
let newSentence = withoutString(sentencePrompt, removePrompt);
console.log(sentencePrompt);
console.log(removePrompt);
console.log(newSentence);
However, my code simply returns the original sentence without removing anything. Can anyone provide some guidance or advice? Thank you!