Javascript 中的嵌套 for 循环问题 - 基本算法脚本
Issue with nested for loops in Javascript - Basic Algorithm Scripting
我正在尝试编写一个接受字符串数组作为参数的函数。我想 return 字符串数组首先按元音数量排序,然后按字母顺序排序。
输入: ['dog', 'cat', 'elephant', 'hippo', 'goat']
输出:['cat'、'dog'、'goat'、'hippo'、'elephant']
function vowelMatching(vowels) {
return vowels.match(/[aeiou]/ig) || [];
//The 'i' in regex accounts for capitals
}
function arrangeByVowels(array) {
const outputArray = [];
console.log(array.length)
console.log(outputArray.length)
for(let x=0; array.length === outputArray.length; x++) {
// let x = 0
const filterArray = [];
for(let i =0; i<array.length; i++) {
//console.log(vowelMatching(array[i]).length)
if (vowelMatching(array[i]).length === x) {
filterArray.push(array[i])
}
}
//console.log(filterArray.sort())
outputArray.push(...filterArray.sort())
}
return outputArray
}
console.log(arrangeByVowels(['dog', 'gAg', 'qq', 'cat', 'elephant', 'hippo', 'goat']))
我正在使用嵌套的 for 循环来实现这一点。如果我为 x
分配一个值,例如 let x = 1
并且我注释掉外部 for 循环,它将 return 所有具有 1 个元音的单词按字母顺序排列。 (这是我想要的结果)
我想遍历 x
直到 array.length === outputArray.length
,但目前这段代码 return 是一个空数组,我不确定为什么。
我想我很接近,但不知道我哪里错了。我做错了什么?
这可以在单个 .sort() 中完成;
let source = ['dog', 'gAg', 'qq', 'cat', 'elephant', 'hippo', 'goat', 'arose', 'unity', 'arise']
let result = source.sort((a,b) => {
let aVowels = a.match(/[aeiou]/gi) || [];
let bVowels = b.match(/[aeiou]/gi) || [];
return aVowels.length === bVowels.length ?
(a > b ? 1 : -1) : // same number of vowels so compare alphabetically
(aVowels.length > bVowels.length ? 1 : -1) // different number of vowels so compare lengths
})
console.log(result);
作为对您实际问题的后续回答....
问题出在您初始 for 循环中的逻辑
for(let x=0; array.length === outputArray.length; x++)
您的条件 array.length === outputArray.length 最初总是 return false 因为您的输出数组与初始数组的长度不同。这将立即结束 for 循环,因为继续迭代的条件已失败。
for循环本质上是while(条件为真)递增x;您的条件开始为假。
要更正它,只需更改条件,使其始终为真,直到满足您的条件:
for(let x=0; outputArray.length < array.length; x++)
function vowelMatching(vowels) {
return vowels.match(/[aeiou]/ig) || [];
}
function arrangeByVowels(array) {
const outputArray = [];
for(let x=0; outputArray.length < array.length; x++) { // Logic error was here.
const filterArray = [];
for(let i =0; i<array.length; i++) {
if (vowelMatching(array[i]).length === x) {
filterArray.push(array[i])
}
}
outputArray.push(...filterArray.sort())
}
return outputArray
}
console.log(arrangeByVowels(['dog', 'gAg', 'porcupine', 'qq', 'alligator', 'cat', 'elephant', 'albatross', 'hippo', 'goat', 'potato', 'banana','aardvark' ]))
我正在尝试编写一个接受字符串数组作为参数的函数。我想 return 字符串数组首先按元音数量排序,然后按字母顺序排序。
输入: ['dog', 'cat', 'elephant', 'hippo', 'goat'] 输出:['cat'、'dog'、'goat'、'hippo'、'elephant']
function vowelMatching(vowels) {
return vowels.match(/[aeiou]/ig) || [];
//The 'i' in regex accounts for capitals
}
function arrangeByVowels(array) {
const outputArray = [];
console.log(array.length)
console.log(outputArray.length)
for(let x=0; array.length === outputArray.length; x++) {
// let x = 0
const filterArray = [];
for(let i =0; i<array.length; i++) {
//console.log(vowelMatching(array[i]).length)
if (vowelMatching(array[i]).length === x) {
filterArray.push(array[i])
}
}
//console.log(filterArray.sort())
outputArray.push(...filterArray.sort())
}
return outputArray
}
console.log(arrangeByVowels(['dog', 'gAg', 'qq', 'cat', 'elephant', 'hippo', 'goat']))
我正在使用嵌套的 for 循环来实现这一点。如果我为 x
分配一个值,例如 let x = 1
并且我注释掉外部 for 循环,它将 return 所有具有 1 个元音的单词按字母顺序排列。 (这是我想要的结果)
我想遍历 x
直到 array.length === outputArray.length
,但目前这段代码 return 是一个空数组,我不确定为什么。
我想我很接近,但不知道我哪里错了。我做错了什么?
这可以在单个 .sort() 中完成;
let source = ['dog', 'gAg', 'qq', 'cat', 'elephant', 'hippo', 'goat', 'arose', 'unity', 'arise']
let result = source.sort((a,b) => {
let aVowels = a.match(/[aeiou]/gi) || [];
let bVowels = b.match(/[aeiou]/gi) || [];
return aVowels.length === bVowels.length ?
(a > b ? 1 : -1) : // same number of vowels so compare alphabetically
(aVowels.length > bVowels.length ? 1 : -1) // different number of vowels so compare lengths
})
console.log(result);
作为对您实际问题的后续回答....
问题出在您初始 for 循环中的逻辑
for(let x=0; array.length === outputArray.length; x++)
您的条件 array.length === outputArray.length 最初总是 return false 因为您的输出数组与初始数组的长度不同。这将立即结束 for 循环,因为继续迭代的条件已失败。
for循环本质上是while(条件为真)递增x;您的条件开始为假。
要更正它,只需更改条件,使其始终为真,直到满足您的条件:
for(let x=0; outputArray.length < array.length; x++)
function vowelMatching(vowels) {
return vowels.match(/[aeiou]/ig) || [];
}
function arrangeByVowels(array) {
const outputArray = [];
for(let x=0; outputArray.length < array.length; x++) { // Logic error was here.
const filterArray = [];
for(let i =0; i<array.length; i++) {
if (vowelMatching(array[i]).length === x) {
filterArray.push(array[i])
}
}
outputArray.push(...filterArray.sort())
}
return outputArray
}
console.log(arrangeByVowels(['dog', 'gAg', 'porcupine', 'qq', 'alligator', 'cat', 'elephant', 'albatross', 'hippo', 'goat', 'potato', 'banana','aardvark' ]))