๐ 1. ๋ฌธ์
[Codewars, 8kyu] Century From Year
The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc.
Task :
Given a year, return the century it is in.
Input , Output Examples ::
centuryFromYear(1705) returns (18)
centuryFromYear(1900) returns (19)
centuryFromYear(1601) returns (17)
centuryFromYear(2000) returns (20)
Hope you enjoy it .. Awaiting for Best Practice Codes
Enjoy Learning !!!
โ ํ์ด
function century(year) {
let result = year / 100;
return Math.ceil(result);
}
- ๋ช์ธ๊ธฐ์ธ์ง ํ์ธํ๋ ํจ์๋ฅผ ์์ฑํ๋ ๋ฌธ์ ์ธ๋ฏ.. 100์ ๋๋ ๋๋จธ์ง๊ฐ ๋์ค๋ฉด
ceil
์ฌ๋ฆผํ๋ ํจ์๋ฅผ ์์ฑ
๐ 2. ๋ฌธ์
[Codewars, 7kyu] Find the divisors!
Create a function named divisors/Divisors that takes an integer n > 1 and returns an array with all of the integerโs divisors(except for 1 and the number itself), from smallest to largest. If the number is prime return the string โ(integer) is primeโ (null in C#) (use Either String a in Haskell and Result<Vec
Example:
divisors(12); // should return [2,3,4,6]
divisors(25); // should return [5]
divisors(13); // should return "13 is prime"
โ ํ์ด
function divisors(integer) {
let result = [];
for (let i = 2; i < integer; i++) {
if (integer % i === 0) {
result.push(i);
}
}
if (result.length === 0) {
return `${integer} is prime`;
}
return result;
}
- 1๊ณผ integer์ ์ ์ธํ, integer์ ์ฝ์๋ฅผ ์ฐพ๋ ๋ฌธ์ ๋ผ๊ณ ์ ๊ทผํ๋ค.. 2์ integer์ ์ฌ์ด์ ์ซ์๋ฅผ ๋ฐ๋ณต๋ฌธ์ ๋๋ ค์ ๋๋ด์ ๋ ๋๋จธ์ง๊ฐ 0์ด๋ฉด ๋ฐฐ์ด์
push
๐ 3. ๋ฌธ์
[Codewars, 6kyu] Multiples of 3 or 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.
Note: If the number is a multiple of both 3 and 5, only count it once. Also, if a number is negative, return 0(for languages that do have them)
โ ํ์ด
function solution(number) {
let result = [];
for (let i = 0; i < number; i++) {
if (i % 3 === 0 || i % 5 === 0) {
result.push(i);
}
}
const result2 = result.reduce(function (prev, curr) {
return prev + curr;
}, 0);
return result2;
}
- ๋ฐฐ์ด์ ์๋ ๋ชจ๋ ์๋ฅผ ๋ํ๊ธฐ ์ํด
reduce
๋ฅผ ์ฌ์ฉํ๋ค. ๊ทธ๋ฐ๋ฐ ์ถ์ฒํ์ด์์๋ result๋ฅผ ๊ทธ๋ฅ 0์ด๋ผ๋ ๋ณ์๋ก ๋๊ณ ๋ฐ๋ณต๋ฌธ์ด ๋๋ ๋ง๋คresult+=i
ํ๋์ฉ ๋ํด์ง๋ ์ฝ๋๊ฐ ์ฌ์ฉ๋๋ค..^^^ ๊ดํ ๋ ์ด๋ ต๊ฒ ์๊ฐํ๋ค..^^