打印不满足条件的元素

时间:2020-10-23 20:10:00

标签: javascript

我正在尝试完成一个HackerRank挑战,其中涉及从所有元音然后是所有辅音开始打印数组的元素列表,但是我遇到了麻烦。

function vowelsAndConsonants(s) {
    var str = s;
    var arr = str.split("");
    var i;
    var vow = ["a","e","i","o","u","y"];
    var con = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z']
    for (i = 0; i < arr.length; i++) {
        for (var j = 0; j< vow.length; j++){
            if (arr[i] === vow[j]){
                console.log(arr[i])
            }
            
        }
    
    }
} 

这是我从上面的代码中获得的内容,但是我无法打印辅音。输入的字符串是“ javascriptloops”

a
a
i
o
o

我也尝试过

function vowelsAndConsonants(s) {
    var str = s;
    var arr = str.split("");
    var i;
    var vow = ["a","e","i","o","u","y"];
    var con = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z']
    for (i = 0; i < arr.length; i++) {
        for (var j = 0; j< vow.length; j++){
            if (arr[i] === vow[j]){
                console.log(arr[i])
                break;
            }
            
        }
    
        for (var j = 0; j< con.length; j++){
            if (arr[i] === con[j]){
                console.log(arr[i])
            }
                
        }
    }
          
}

但这就是我的生活

j
a
v
a
s
c
r
i
p
t
l
o
o
p
s

6 个答案:

答案 0 :(得分:1)

最好定义较小的子集(元音),并将其余字母视为辅音。

此外,为变量命名对可读性有很大帮助

function vowelsAndConsonants(s) {
  var letters = s.split("");
  const vowels = ["a", "e", "i", "o", "u", "y"];
  for (const letter of letters) { // we are cycling through the letters to find all vowels
    for (const vowel of vowels) { // now we are cycling through the list of vowels
      if (letter === vowel) { // if we found a vowel in the vowel list, print it and stop searching for other vowels (break)
        console.log(letter);
        break;
      }
    }
  }

  // Searching for consonants is trickier
  for (const letter of letters) { // we cycle again through the letters
    let consonant = true; // we assume that the letter is consonant until it's proven otherwise

    for (const vowel of vowels) { // cycling through the list of vowels
      if (letter === vowel) { // if we found that the letter is vowel
        consonant = false; // we set the flag to false
        break; // and stop searching more as we know already that it is a vowel
      }
    }

    if (consonant === true) { // if after cycling we realized that this is a consonant, and not a vowel - print it
      console.log(letter)
    }
  }
}

vowelsAndConsonants("normal");

答案 1 :(得分:1)

一个简单的解决方案将循环两次:

function vowelsAndConsonants(s) {
    let vowels = ["a", "e", "i", "o", "u"];

    for(let v of s) {
        if(vowels.includes(v))
            console.log(v);
    }
    for(let v of s) {
        if(!vowels.includes(v))
            console.log(v);
    }
}

答案 2 :(得分:0)

有解决方案,你可以看看

function vowelsAndConsonants(s) {
    var str = s;
    var arr = str.split("");
    var vow = ["a","e","i","o","u","y"];
   
    
    const ordered = arr.reduce( (res, letter) => {
      if (vow.includes(letter)) {
        return {...res, vow: [...res.vow, letter]}
      }

      return {...res, con: [...res.con, letter]}
    }, { con: [], vow: []});
    
    return ordered.vow.join('') + ordered.con.join('');
} 

答案 3 :(得分:0)

您的外循环需要运行两次。

此解决方案还使用for ... in循环和array.includes(),这比嵌套循环的标准要干净一些。

const vowelsAndConsonants = s => {
  const vow = ['a','e','i','o','u','y'];
  for (const c of s)
    if (vow.includes(c))
      console.log(c)
  for (const c of s)
    if (!vow.includes(c))
      console.log(c)
}
    
vowelsAndConsonants('javascriptloops')

答案 4 :(得分:0)

const f = s => {
  const isVowel = char => ['a', 'e', 'i', 'o', 'u'].includes(char);
  const letters = s.split('');
  const consonants = letters.reduce((result, letter) => {
    if (isVowel(letter)) console.log(letter);
    else result.push(letter);
    return result;
  }, []);
  consonants.forEach(c => console.log(c))
}

f('javascriptloops');

答案 5 :(得分:0)

您可以使用filter()代替循环:

var vow = ['a','e','i','o','u','y'].join('');
var str = 'javascriptloops'.split('');

console.log(str.filter(x => vow.includes(x)));   // vowels
console.log(str.filter(x => !vow.includes(x)));  // consonants (not vowels)