# [Codewars - 6kyu解題] Multiples of 3 or 5 加總3或5的倍數 ###### tags: `Codewars`,`6kyu`,`Javascript`,`Array`,`for-loop` > Javascript菜鳥紀錄Codewars解題過程 ## Instructions 題目 :link: https://www.codewars.com/kata/514b92a657cdc65150000006 :pushpin: **Instructions:** 接收一個數字,回傳小於該數字的所有自然數中是3或5的倍數的數字總和。 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. :bulb: **Notice:** 若同時是3和5的倍數,只要計算一次即可。 If the number is a multiple of both 3 and 5, only count it once. ## My Solution 我的解法 ```javascript= function solution(number){ var arr = [] var res = 0 for(i=0;i<number;i++){ if(i%3==0||i%5==0){arr.push(i);} } arr.forEach(i => res+=i); return res } ``` ## Solutions 其他更精簡的寫法 ```javascript= function solution(number){ var sum = 0; for(var i = 1;i< number; i++){ if(i % 3 == 0 || i % 5 == 0){ sum += i } } return sum; } ```