代码之家  ›  专栏  ›  技术社区  ›  00Saad

javascript:string不像我期望的那样具体化

  •  1
  • 00Saad  · 技术社区  · 7 年前

    “快速棕色狐狸”

    “伊莎伊·伊克奎·奥恩布雷·奥克斯菲”
    “伊塔伊·伊克奎·奥恩布雷·奥克费伊”

    代码

    if (str.match(/[ ]/)) {
    
            str = str.split(" "); 
            for (let i = 0; i < str.length; i++) {
                for (let j = 0; j < str.length; j++) {
                    if (str[j].match(/^[q][u]/)) str[j] = str[j]
                        .substring(2) + str[j].slice(0, 2);
                    if (str[j].match(/^[^aeiou]/)) {
                        str[j] = str[j].substring(1) + str[j].slice(0, 1);
                    }
                }
    
                str[i] = str[i] + 'ay';
            }
    
            str = str.join(" ");
    
        }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Joseph Marikle    7 年前

    你没有抓住你的所有辅音 substring/slice

    str = "the quick brown fox".split(" "); 
    for (let i = 0; i < str.length; i++) {
        for (let j = 0; j < str.length; j++) {
            if (str[j].match(/^qu/)) str[j] = str[j]
                .substring(2) + str[j].slice(0, 2);
            if (match = str[j].match(/^[^aeiou]+/)) {
                let charCount = match.toString().length;
                str[j] = str[j].substring(charCount) + str[j].slice(0, charCount);
            }
        }
    
        str[i] = str[i] + 'ay';
    }
    
    str = str.join(" ");
    
    console.log(str);
        2
  •  0
  •   cwallenpoole    7 年前

    ay the 从两个辅音开始,h在第二次迭代中被推到末尾。

    str = "the extra quick brown fox"
    if (str.match(/[ ]/)) {
        str = str.split(" "); 
        for (let i = 0; i < str.length; i++) {
            for (let j = 0; j < str.length; j++) {
                if (str[j].match(/^[q][u]/)) str[j] = str[j]
                    .substring(2) + str[j].slice(0, 2);
                if (str[j].match(/^[^aeiou]/)) {
                    str[j] = str[j].substring(1) + str[j].substring(0,1);
                }
            }
            console.log(str[i])
    
            str[i] = str[i] + 'ay';
            console.log(str)
        }
        str = str.join(" ");
    
    }
    VM383:12 het
    // Notice it is correct with the first pass.
    VM383:15 (5) ["hetay", "extra", "ickqu", "rownb", "oxf"]
    VM383:12 extra
    // but it is incorrect after the second.
    VM383:15 (5) ["etayh", "extraay", "ickqu", "ownbr", "oxf"]