【问题标题】:JavaScript string converting to lower or uppercaseJavaScript 字符串转换为小写或大写
【发布时间】:2018-09-07 12:06:43
【问题描述】:
//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] 将是我的第一个项目。我不知道这是否可行,但至少我需要尝试。即使使用正则表达式,任何帮助都将不胜感激。谢谢

【问题讨论】:

标签: javascript arrays replace split


【解决方案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("") &amp;&amp; 做什么……别管它了。

【讨论】:

    【解决方案2】:

    这是一个满足13 的快速函数。我不确定你在 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(" ");
    }

    【讨论】:

    • 第四种情况我需要输入 "eeeeee teeeeee" = "ee Tee" 输出非常感谢
    • 所以本质上,当同一个元音重复多次时,您希望最多保留两次出现。这是正确的@EmirMustafoski 吗?
    • 是的@Angel Politis
    • 查看我的更新@EmirMustafoski。我希望你不要费心使用一个小的正则表达式来做到这一点。否则会相当复杂。
    • 它实际上并没有显示正确的第一个测试(我的意思是字符串)上面这个带有reduce的人具备所有条件。顺便说一句,在此之前它适用于前 3 个条件 - @Angel Politis
    【解决方案3】:

    你可以使用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);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-18
      • 2016-01-22
      • 2020-12-05
      • 2010-11-04
      • 2010-10-09
      • 2014-03-30
      • 2016-01-25
      相关资源
      最近更新 更多