分别打印元音和辅音的代码不起作用 [英] Code for printing vowels and consonants separately not working
本文介绍了分别打印元音和辅音的代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入一个字符串。然后执行:
每个字母都打印在新行上。
然后,按照与中出现的顺序打印元音。
然后,辅音按照它们出现的顺序打印
Input a string. Then perform:: Each letter is printed on a new line. Then the vowels are printed in the same order as they appeared in . Then the consonants are printed in the same order as they appeared in
样本输入
javascriptloops
样本输出
a
a
i
o
o
j
v
s
c
r
p
t
l
p
s
这就是我所做的
let a = [];
let b = [];
vowelsAndConsonants("javascriptloops");
function vowelsAndConsonants(s) {
let i;
let k = 0,
j = 0;
s.trim();
s.toLowerCase();
for (i = 0; i <= s.length; i++) {
if (s.charAt(i) === "a" || s.charAt(i) === "e" || s.charAt(i) === "i" || s.charAt(i) === "o" || s.charAt(i) === "u") {
a[k] = s.charAt(i);
k++;
} else {
b[j] = s.charAt(j);
j++;
}
}
}
for (let i = 0; i <= a.length; i++) {
console.log(a[i]);
}
for (let i = 0; i <= b.length; i++) {
console.log(b[i]);
}
推荐答案
执行此操作的最佳方法是使用正则表达式匹配元音和辅音,然后打印每个元音和辅音。
The best way to do this is using regular expression to match vowels and consonants, and then print each of them.
下面是工作代码段:
Below is the working code snippet:
function vowelsAndConsonants(s) {
var vw =s.match(/[aeiouAEIOU]+?/g); //regular expression to match vowels
var con=s.match(/[^aeiouAEIOU]+?/g); //regular expression to not match vowels, ie. to match consonants
printOnConsole(vw); //print vowels
printOnConsole(con); //print consonants
}
//function to print values on console.
function printOnConsole(arrPrint){
for(var i=0;i<arrPrint.length;i++){
console.log(arrPrint[i]);
}
}
这篇关于分别打印元音和辅音的代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文