I'm trying to make a function that will delete "negative" responses in a magic 8 ball application. I'm trying to delete any elements of the response array that have the word "no" in it but I dont want to delete responses that include "now" or "not". The array looks like the following
const responses = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Can't predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"
]
const negativeResponseDeleter = () => {
let splitTracker = 0
let holdingArray = []
for (i = 0; i < responses.length; i ) {
holdingArray = responses[i].split(" ")
if (holdingArray.includes("no")) {
}
}
}
Above is the code I have so far but I keep running into roadblocks, wondering what the best way to go about this would be. I'm very new to this so any formatting advice would also be appreciated.
CodePudding user response:
You can store the words in words
and inverse the condition (if any of the words in the response don't contain no, add it to the array) then return the array.
const responses = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Can't predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"
]
const negativeResponseDeleter = () => {
let holdingArray = []
for (i = 0; i < responses.length; i ) {
const words = responses[i].split(" ")
if (!words.includes("no")) {
holdingArray.push(responses[i])
}
}
return holdingArray
}
console.log(negativeResponseDeleter())
CodePudding user response:
You could create a regular expression that tests to see if a string contains "no" surrounded by word boundaries (\b
). Then use filter
to return only those strings that don't pass the test.
const responses=["It is certain","It is decidedly so","Without a doubt","Yes, definitely","You may rely on it","As I see it, yes","Most likely","Outlook good","Yes","Signs point to yes","Reply hazy try again","Ask again later","Better not tell you now","Can't predict now","Concentrate and ask again","Don't count on it","My reply is no","My sources say no","Outlook not so good","Very doubtful"];
const re = /\bno\b/;
const output = responses.filter(response => {
return !re.test(response);
});
console.log(output);
CodePudding user response:
You can use an array filter with a regex that looks for word boundaries:
const responses = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Can't predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"
];
const negativeResponseDeleter = () => {
let holdingArray = responses.filter(str => !/\bno\b/.test(str));
console.log(holdingArray)
}
negativeResponseDeleter();
Output:
['It is certain', 'It is decidedly so', 'Without a doubt', 'Yes, definitely', 'You may rely on it', 'As I see it, yes', 'Most likely', 'Outlook good', 'Yes', 'Signs point to yes', 'Reply hazy try again', 'Ask again later', 'Better not tell you now', "Can't predict now", 'Concentrate and ask again', "Don't count on it", 'Outlook not so good', 'Very doubtful']