我有一個函式threeOrFive(max)。它應該回圈每個可以被 3 或 5 整除但不能同時被整除的數字。所以它不會回圈 15,因為它可以被兩個數字整除,但會列印出“3, 5, 6, 9, 10, 12, 18”。
我試過的代碼是
function threeOrFive(max){
for(let i = 0; i<max; i =3, i =5){
console.log(i);
}
}
和
function threeOrFive(max){
for(let i = 0; i<max; i =3){
if(i =3 % max === 0 && i =5 % max === 0){
return false;
}
console.log(i);
}
}
uj5u.com熱心網友回復:
您可以使用此功能。這將回傳一個包含所有可被 3 或 5 整除但不能同時被兩者整除的數字的陣列。
const threeOrFive = max => {
const store = [];
for(let i = 0; i < max; i ){
if (i%3==0 && i%5==0) continue;
else if (i%3==0) store.push(i);
else if (i%5==0) store.push(i);
}
return store;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/473816.html
標籤:javascript 循环
