2 回答
TA贡献1863条经验 获得超2个赞
问题不是很清楚,但是看看这个:
const arr1 = [222, 223, 225 ,227, 232, 233, 235, 237,252, 253, 255, 257, 272, 273, 275, 277, 322, 323, 325, 327, 332, 333, 335, 337, 352, 353, 355, 357, 372, 373, 375, 377, 522, 523, 525, 527, 532, 533, 535, 537, 552,553,555, 557, 572, 573, 575, 577, 722, 723, 725, 727, 732, 733, 735, 737, 752, 753, 755, 757, 772, 773, 775, 777];
const arr2 = [22, 23, 25, 27, 32, 33,35, 37, 52, 53, 55, 57, 72, 73, 75,77];
const products = [];
arr1.forEach(q => {
arr2.forEach(v => {
let n = q*v, s = n.toString();
if(s.length === 5 && s.match(/^[2357]+$/))products.push(n);
});
});
console.log(products);
TA贡献1865条经验 获得超7个赞
使用Array.filter()和Array.some这里。请参阅内联评论。
let arr1 = [222, 223, 225 ,227, 232, 233, 235, 237,252, 253, 255, 257, 272, 273, 275, 277, 322, 323, 325, 327, 332, 333, 335, 337, 352, 353, 355, 357, 372, 373, 375, 377, 522, 523, 525, 527, 532, 533, 535, 537, 552,553,555, 557, 572, 573, 575, 577, 722, 723, 725, 727, 732, 733, 735, 737, 752, 753, 755, 757, 772, 773, 775, 777]
let arr2 = [22, 23, 25, 27, 32, 33,35, 37, 52, 53, 55, 57, 72, 73, 75,77];
let primes = [2,3,5,7];
products = []
for (var i=0; i< arr1.length; i++){
for (var j=0; j< arr2.length; j++){
products.push(arr1[i]*arr2[j])
}
}
// Return a filtered array of products that only contains
// items from the original array that pass the provided callback
// function's tests.
let results = products.filter(function(item){
// Test to see if item contains primes
let prime = Array.from(item.toString()).some(function(char){
return primes.indexOf(+char);
});
// Return item only if contains a prime and has 5 digits
return prime && item.toString().length === 5 ? item : null;
});
console.log(results);
添加回答
举报