我有像这样的javascript代码,如何让它更容易阅读,速记和高效:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; var count = 0; for (i in numbers) { value = numbers[i]; if (value % 2 == 0) { count++; if (count == 5) { console.log("The 5th even number is " + value); } if (count == 7) { console.log("The 7th even number is " + value); } } } count = 0; for (i in numbers) { value = numbers[i]; if (value % 2 == 1) { count++; if (count == 3) { console.log("The 3rd odd number is " + value); } if (count == 8) { console.log("The 8th odd number is " + value); } } } var odds = [], evens = []; for (i in numbers) { value = numbers[i]; if (value % 2 == 1) { odds.push(value); } else if (value % 2 == 0) { evens.push(value);`enter code here` } } console.log('The odd numbers: ' + odds.join(", ")); console.log('The even numbers: ' + evens.join(", "));
并且这些代码的结果如下:
第五个偶数是10 第7个偶数是14 第三个奇数是5 第8个奇数是15 奇数:1,3,5,7,9,11,13,15,17,19 偶数:2,4,6,8,10,12,14,16,18,20
答案 0 :(得分:1)
不是循环遍历同一个数组两次并检查平均值和赔率,你可以循环一次并保留你的if语句为evens但是和else将是所有奇数。在您完成此循环的同时,您可以将这些数字分配给您的单个evens / odds数组,而不是再次循环。如果你想让它更具动态性,你可以添加一个如下所示的函数来获取你想要进行控制台日志的数字的后缀。
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
ordinalSuffixOf = function(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
},
countEven = 0,
countOdd = 0,
odds = [],
evens = [];
for (i in numbers) {
value = numbers[i];
if (value % 2 == 0) {
evens.push(value);
countEven++;
if (countEven == 5 || countEven == 7) {
console.log("The " + countEven + ordinalSuffixOf(countEven) + " even number is " + value);
}
} else {
odds.push(value);
countOdd++;
if (countOdd == 3 || countOdd == 8) {
console.log("The " + countOdd + ordinalSuffixOf(countOdd) + " odd number is " + value);
}
}
}
console.log('The odd numbers: ' + odds.join(", "));
console.log('The even numbers: ' + evens.join(", "));
答案 1 :(得分:1)
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
重复了很多“检查均匀”逻辑,因此我们可以创建一个辅助方法来处理它。
var isEven = function(number) {
return number % 2 === 0;
};
我们可以使用Array.prototype.filter
来获取偶数,然后使用Array.prototype.forEach
来迭代这些偶数。 forEach()
甚至可以很好地跟踪我们正在使用的#th号码!
numbers.filter(isEven)
.forEach(function(number, index) {
if(index == 5 || index == 7) {
console.log(`The ${index}th even number is ${number}`);
}
});
numbers.filter(!isEven)
.forEach(function(number, index) {
if(index == 3) {
console.log(`The 3rd odd number is ${number}`);
} else if(index == 8) {
console.log(`The 8th odd number is ${number}`);
}
});
再次,使用Array.prototype.filter
,我们不必在将数据推入数组时摸索。
var odds = numbers.filter(!isEven);
var evens = numbers.filter(isEven);
console.log(`The odd numbers: ${odds.join(', ')}`);
console.log(`The even numbers: ${evens.join(', ')}`);
答案 2 :(得分:0)
我会给你一些提示。
首先,您可以创建一个循环,为您的数字数组添加数字,而不是手动输入您想要的数字。
var numbers = [];
for(var i = 1; i <= 20; i++){
numbers.push(i);
}
其次,而不是:
if (value % 2 == 1) {
odds.push(value);
} else if (value % 2 == 0) {
evens.push(value);`enter code here`
}
你可以这样缩短它:
(value % 2 == 1) ? odds.push(value) : evens.push(value);
有关这些运算符含义的更多信息,请参阅this Stackoverflow answer。
答案 3 :(得分:0)
由于您使用的是一系列1-20数字数组,您可以通过单个for循环来完成,希望此代码可以帮助您
var evenCount = 0,
oddCount = 0,
odds = [],
evens = [];
for (var i = 1; i <= 20; i++) {
if (i % 2 == 0) {
evenCount++;
evens.push(i);
if (evenCount == 5) {
console.log("The 5th even number is " + i);
}
if (evenCount == 7) {
console.log("The 7th even number is " + i);
}
}
else if (i % 2 == 1) {
oddCount++;
odds.push(i);
if (oddCount == 3) {
console.log("The 3rd odd number is " + i);
}
if (oddCount == 8) {
console.log("The 8th odd number is " + i);
}
}
}
console.log('The odd numbers: ' + odds.join(", "));
console.log('The even numbers: ' + evens.join(", "));