//1. input words beginning and ending with a vowel are preserved in lowercase
//2. input words beginning with vowels and ending in a non vowel are translated to uppercase
//3. input words beginning with a non vowel and ending with a vowel are translated to a capitalized word (only first letter is uppercase)
//4. when printing the words, only two vowels are preserved per word (the first two)
//Output "i dont know Dude i AM not typing ALL OF this Nonsens text"
var str = "i dont know dude i am not typing all of this nonsense text";
console.log(str);
var res = str.split(" ");
console.log(res);
for(let i = 0; i<res.length;i++){
//checking words after spliting into single words
if((res[i].split("") && (res[0] ==== 'a'|| 'e' || 'o' || 'u' || 'i' || 'y') && (/* last charachter to check */ ))
}
我是初学JavaScript开发人员,我在练习时遇到一些困难我有以上4个条件首先我将数组拆分成一个单词然后我希望分成单个字符,所以res [0]将是我的第一个项目。我不知道这是否有效,但至少我需要尝试。即使是正则表达式,任何帮助都将非常受欢迎。谢谢
答案 0 :(得分:2)
您可以使用Array.prototype的reduce
。
var str = "i dont know dude i am not typing all of this nonsense text";
console.log(str);
var res = str.split(" ");
var y=res.reduce((a,e) => {
if("aeiou".includes(e[0])){ //condition 1 and 2
return a + ("aeiou".includes(e[e.length-1]) ? getWord(e).toLowerCase() : getWord(e).toUpperCase()) + " ";
}
else{ //condition 3
return a + ("aeiou".includes(e[e.length-1]) ? e[0].toUpperCase() + getWord(e.substr(1)).toLowerCase() : e) + " ";
}
},"");
function getWord(x){ //condition 4
var count = 0;
for(var i = 0; i < x.length; i++){
count += "aeiou".includes(x[i]) ? 1 : 0;
if(count === 3)
return x.substr(0,i);
}
return x;
}
console.log(y);
&#13;
答案 1 :(得分:1)
res[0] ==== 'a'|| 'e'
那不行。你需要这样做:
res[0] ==== 'a'|| res[0] === 'e'
然而,由于这很复杂,可能只会这样做:
"aeiou".includes(res[0])
哦,res[0]
是第一个单词,而不是第一个单词,res[i][0]
。您可以使用res[i][res[i].length - 1]
获取最后一个。而且我仍然没有得到你想要用res[i].split("") &&
做的事情......就这样离开。
答案 2 :(得分:1)
这是一个快速履行1
到3
的功能。我不确定你在4
中的意思。
<强>段:强>
/* Example. */
var str = "i dont knoooow dudeeeee i am not typing all of this nonsense text";
console.log(str);
console.log(process(str));
/* --------------------------------------------- */
/* The function that checks whether a char is a vowel. */
function isVowel (char) {
return "aeiou".includes(char);
}
/* The function that truncates a word before the 3rd vowel. */
function truncate (word) {
/* Create an index and a counter and save the length of the word. */
var i = 0, c = 0, l = word.length;
/* Iterate over every char until 3 vowels are found, if found. */
for (; i < l && c < 3; i++, c += +isVowel(word[i]));
/* Return the part of the string until before the 3rd vowel. */
return word.slice(0, i);
};
/* The function that processes the string. */
function process (str) {
/* Split the sentence into an array of words. */
var words = str.split(" ");
/* Iterate over every word. */
words.forEach(function (word, index) {
/* Strip any more than 2 repeated vowels and anything beyond the 3rd vowel. */
word = truncate(word.replace(/([aeiou])\1{2,}/i, "$1$1")); // 4
/* Check whether the first character is a vowel or not. */
words[index] = isVowel(word[0])
/* Check whether the last character is a vowel or not. */
? isVowel(word[word.length - 1])
? word.toLowerCase() // 1
: word.toUpperCase() // 2
/* Check whether the last character is a vowel or not. */
: isVowel(word[word.length - 1])
/* Capitalise the first char and turn the rest to lowercase. */
? word[0].toUpperCase() + word.slice(1).toLowerCase() // 3
: word;
});
/* Join the array into a string and return it. */
return words.join(" ");
}
&#13;