/*
https://school.programmers.co.kr/learn/courses/30/lessons/12910

array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ
arr์€ ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
์ •์ˆ˜ i, j์— ๋Œ€ํ•ด i ≠ j ์ด๋ฉด arr[i] ≠ arr[j] ์ž…๋‹ˆ๋‹ค.
divisor๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
array๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…
์ž…์ถœ๋ ฅ ์˜ˆ#1
arr์˜ ์›์†Œ ์ค‘ 5๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์›์†Œ๋Š” 5์™€ 10์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [5, 10]์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ#2
arr์˜ ๋ชจ๋“  ์›์†Œ๋Š” 1์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ์›์†Œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด [1, 2, 3, 36]์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ#3
3, 2, 6์€ 10์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์›์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ [-1]์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
*/
const solution = (arr, divisor) => {
  const answer = [];

  // for๋ฌธ ์Šคํƒ€๋šœ
  for (let i = 0; i < arr.length; i++) {
    // ๋ฐฐ์—ด ๊ฐ’ ํ•˜๋‚˜ํ•˜๋‚˜๊ฐ€ 0์œผ๋กœ ๋‚˜๋ˆ ์ง„๋‹ค๋ฉด ๋นˆ ๋ฐฐ์—ด answer ์— ํ‘ธ์‰ฌ
    if (arr[i] % divisor === 0) {
      answer.push(arr[i]);
    }
  }

  // 1์ฐจ๋กœ ํ‘ธ์‰ฌ๋œ answer์— ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ answer ๋ฐฐ์—ด๊ฐ’์ด ๋น„์–ด์žˆ์œผ๋ฉด -1์„ ํ‘ธ์‰ฌํ•˜๊ณ ,
  // ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ sorting
  answer.length === 0 ? answer.push(-1) : answer.sort((a, b) => a - b);

  return answer;
};

console.log(solution([5, 9, 7, 10], 5));
console.log(solution([2, 36, 1, 3], 1));
console.log(solution([3, 2, 6], 10));

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ sorting ํ•˜๋Š” ๊ณผ์ •์—์„œ ์‚ด์ง ์• ๋จน์€ ๋ฌธ์ œ;;

๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ sorting์„ ํ–ˆ๋”๋‹ˆ, ๋‘๋ฒˆ์งธ ์ผ€์ด์Šค๊ฐ€ ์ž๊พธ output์œผ๋กœ [1, 2, 36, 3]์ด ๋‚˜์™€์„œ ๊ณ ์ƒํ–ˆ๋‹ค.

๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค!