๐Ÿ“Œ 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, String> in Rust).

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 ํ•˜๋‚˜์”ฉ ๋”ํ•ด์ง€๋Š” ์ฝ”๋“œ๊ฐ€ ์‚ฌ์šฉ๋๋‹ค..^^^ ๊ดœํžˆ ๋” ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ–ˆ๋„ค..^^