I am trying to write a function that will remove all vowels in a given string in JS. I understand that I can just write string.replace(/[aeiou]/gi,"") but I am trying to complete it a different way...this is what I have so far... thank you!
我正在尝试编写一个函数,它将删除JS中给定字符串中的所有元音。我知道我可以写string.replace(/[aeiou]/gi,")但我正尝试用另一种方式完成它……这就是我目前所拥有的……谢谢你们!
I first made a different function called IsaVowel that will return the character if it is a vowel...
我首先做了一个不同的函数IsaVowel,如果是元音,它会返回字符……
function withoutVowels(string) {
var withoutVowels = "";
for (var i = 0; i < string.length; i++) {
if (isaVowel(string[i])) {
***not sure what to put here to remove vowels***
}
}
return withoutVowels;
}
2 个解决方案
#1
6
Use accumulator pattern.
使用累加器模式。
function withoutVowels(string) {
var withoutVowels = "";
for (var i = 0; i < string.length; i++) {
if (!isVowel(string[i])) {
withoutVowels += string[i];
}
}
return withoutVowels;
}
function isVowel(char) {
return 'aeiou'.includes(char);
}
console.log(withoutVowels('Hello World!'));
#2
0
I tried doing this problem by first splitting the string into an array, while also creating an array of vowels. Then go through each element in the string array and check whether it's in my vowel array. If it is not in my vowel array, push it to the withoutVowels array. At the end of the for loop, join all elements in the withoutvowels array and return.
我试着先把这个字符串分割成一个数组,同时创建一个元音数组。然后遍历字符串数组中的每个元素,并检查它是否在我的元音数组中。如果它不在我的元音数组中,将它推到without元音数组。在for循环结束时,将不带元音的数组中的所有元素联接并返回。
function withoutVowels(string) {
var strWithoutVowels = [];
string = string.split('');
var vowels = ['a', 'e', 'i', 'o', 'u'];
for (var i = 0; i < string.length; i++) {
if (vowels.indexOf(string[i]) < 0) {
strWithoutVowels.push(string[i])
}
}
strWithoutVowels = strWithoutVowels.join('');
return strWithoutVowels;
}
console.log(withoutVowels('Hello World!'))
#1
6
Use accumulator pattern.
使用累加器模式。
function withoutVowels(string) {
var withoutVowels = "";
for (var i = 0; i < string.length; i++) {
if (!isVowel(string[i])) {
withoutVowels += string[i];
}
}
return withoutVowels;
}
function isVowel(char) {
return 'aeiou'.includes(char);
}
console.log(withoutVowels('Hello World!'));
#2
0
I tried doing this problem by first splitting the string into an array, while also creating an array of vowels. Then go through each element in the string array and check whether it's in my vowel array. If it is not in my vowel array, push it to the withoutVowels array. At the end of the for loop, join all elements in the withoutvowels array and return.
我试着先把这个字符串分割成一个数组,同时创建一个元音数组。然后遍历字符串数组中的每个元素,并检查它是否在我的元音数组中。如果它不在我的元音数组中,将它推到without元音数组。在for循环结束时,将不带元音的数组中的所有元素联接并返回。
function withoutVowels(string) {
var strWithoutVowels = [];
string = string.split('');
var vowels = ['a', 'e', 'i', 'o', 'u'];
for (var i = 0; i < string.length; i++) {
if (vowels.indexOf(string[i]) < 0) {
strWithoutVowels.push(string[i])
}
}
strWithoutVowels = strWithoutVowels.join('');
return strWithoutVowels;
}
console.log(withoutVowels('Hello World!'))