Console.log 不工作,我不知道为什么
Console.log is not working and I don't know why
我在概念上不理解为什么 console.log 会抛出错误。
错误是:Uncaught TypeError: Cannot read 属性 'toLowerCase' of undefined
在重新排列 (:6:18)
在 :13:1
let vowels = [];
let consonants = [];
let s = "ellothere";
function rearranger(input) {
for (let i = 0; i <= input.length; i++) {
if (s[i].toLowerCase() === "a" || s[i].toLowerCase() === "e" || s[i].toLowerCase() === "i" || s[i].toLowerCase() === "o" || s[i].toLowerCase() === "u") {
vowels = [...vowels, s[i]];
} else {
consonants = [...consonants, s[i]];
}
}
}
rearranger(s);
console.log(vowels, consonants);
将 for
循环更改为:
for (let i = 0; i < input.length; i++)
或
for (let i = 0; i <= input.length -1 ; i++)
否则,您将遍历字符串末尾并收到错误。
我在概念上不理解为什么 console.log 会抛出错误。
错误是:Uncaught TypeError: Cannot read 属性 'toLowerCase' of undefined 在重新排列 (:6:18) 在 :13:1
let vowels = [];
let consonants = [];
let s = "ellothere";
function rearranger(input) {
for (let i = 0; i <= input.length; i++) {
if (s[i].toLowerCase() === "a" || s[i].toLowerCase() === "e" || s[i].toLowerCase() === "i" || s[i].toLowerCase() === "o" || s[i].toLowerCase() === "u") {
vowels = [...vowels, s[i]];
} else {
consonants = [...consonants, s[i]];
}
}
}
rearranger(s);
console.log(vowels, consonants);
将 for
循环更改为:
for (let i = 0; i < input.length; i++)
或
for (let i = 0; i <= input.length -1 ; i++)
否则,您将遍历字符串末尾并收到错误。