找不到如何删除输出末尾的 'undefined'
Can't find out how to remove 'undefined' at the end of the output
基本上,练习要求我先将字符串的字符排序为元音,然后是辅音
示例输入
javascriptloops
示例输出
a
a
i
o
o
j
v
s
c
r
p
t
l
p
s
这是我试过的功能:
function vowelsAndConsonants(s) {
let vowel = "";
for (let i = 0; i < s.length; i++) {
if (s[i] === "a" || s[i] === "e" || s[i] === "i" || s[i] === "o" || s[i] === "u") {
vowel += s[i] + "\n";
}
}
for (let i = 0; i < s.length -1 ; i++) {
if (s[i] !== "a" && s[i] !== "e" && s[i] !== "i" && s[i] !== "o" && s[i] !== "u") {
vowel += s[i] + "\n";
}
}
for (let i = s.length -1; i<= s.length; i++) {
if (s[i] !== "a" && s[i] !== "e" && s[i] !== "i" && s[i] !== "o" && s[i] !== "u") {
vowel += s[i] ;
}
}
console.log(vowel)
}
vowelsAndConsonants('javascriptloops')
的输出是:
a
a
i
o
o
j
v
s
c
r
p
t
l
p
sundefined
如何摆脱这个 'undefined'?我知道它来自 console.log
但他们要我用它打印输出。谢谢!
答案已经在评论中提供了(需要使用<
而不是<=
,因为str[str.length]是undefined
),但我会想为您的问题提出一个更具可读性的代码:
const vowFirst = (input) => {
const arr = input.split(''); // Create an array from the string, to loop it easier
const vowels = ['a', 'e', 'i', 'o', 'u']; // Also create an array from the chars you want to treat differently
return [
...arr.filter(char => vowels.includes(char)), // get an array of all the chars in the original array that are vowels (in order)
...arr.filter(char => !vowels.includes(char)) // get an array of all the chars in the original array that are NOT vowels (in order)
].join('\n'); // join the array back to a string (with \n)
}
console.log(vowFirst('javascriptloops'));
我建议您阅读 Mozilla Devs 上的数组函数,因为它们可以使您的代码更具可读性,并且比手动循环或类似方法更容易处理。
此外,我建议您在我链接的同一页面上查看 JS 的基础知识(例如我示例中使用的 Spread Operator)。
基本上,练习要求我先将字符串的字符排序为元音,然后是辅音
示例输入
javascriptloops
示例输出
a
a
i
o
o
j
v
s
c
r
p
t
l
p
s
这是我试过的功能:
function vowelsAndConsonants(s) {
let vowel = "";
for (let i = 0; i < s.length; i++) {
if (s[i] === "a" || s[i] === "e" || s[i] === "i" || s[i] === "o" || s[i] === "u") {
vowel += s[i] + "\n";
}
}
for (let i = 0; i < s.length -1 ; i++) {
if (s[i] !== "a" && s[i] !== "e" && s[i] !== "i" && s[i] !== "o" && s[i] !== "u") {
vowel += s[i] + "\n";
}
}
for (let i = s.length -1; i<= s.length; i++) {
if (s[i] !== "a" && s[i] !== "e" && s[i] !== "i" && s[i] !== "o" && s[i] !== "u") {
vowel += s[i] ;
}
}
console.log(vowel)
}
vowelsAndConsonants('javascriptloops')
的输出是:
a
a
i
o
o
j
v
s
c
r
p
t
l
p
sundefined
如何摆脱这个 'undefined'?我知道它来自 console.log
但他们要我用它打印输出。谢谢!
答案已经在评论中提供了(需要使用<
而不是<=
,因为str[str.length]是undefined
),但我会想为您的问题提出一个更具可读性的代码:
const vowFirst = (input) => {
const arr = input.split(''); // Create an array from the string, to loop it easier
const vowels = ['a', 'e', 'i', 'o', 'u']; // Also create an array from the chars you want to treat differently
return [
...arr.filter(char => vowels.includes(char)), // get an array of all the chars in the original array that are vowels (in order)
...arr.filter(char => !vowels.includes(char)) // get an array of all the chars in the original array that are NOT vowels (in order)
].join('\n'); // join the array back to a string (with \n)
}
console.log(vowFirst('javascriptloops'));
我建议您阅读 Mozilla Devs 上的数组函数,因为它们可以使您的代码更具可读性,并且比手动循环或类似方法更容易处理。
此外,我建议您在我链接的同一页面上查看 JS 的基础知识(例如我示例中使用的 Spread Operator)。