I have requirement like if the given string contains vowels it should print the founded vowels , if it does not contain vowels it should print message like no vowels found.
function findVowels(str) {
const vowels = ['a', 'e', 'i', 'o', 'u'];
const foundVowels = str.contains(vowels);
if (foundVowels.length > 0) {
console.log('Found vowels:', foundVowels.join(', '));
} else {
console.log('No vowels found.');
}
}
const inputString = "Hello, World!";
findVowels(inputString);
>Solution :
I found one solution for your requirement. You need to modify your code like below.
function findVowels(str) {
// Convert the string to lowercase to make the check case-insensitive
str = str.toLowerCase();
// Define an array of vowels
const vowels = ['a', 'e', 'i', 'o', 'u'];
// Filter the characters in the string that are vowels
const foundVowels = Array.from(str).filter(char => vowels.includes(char));
if (foundVowels.length > 0) {
console.log('Found vowels:', foundVowels.join(', '));
} else {
console.log('No vowels found.');
}
}
// Example usage:
const inputString = "Hello, World!";
findVowels(inputString);