๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ตœ๋Œ“๊ฐ’ ๋งŒ๋“ค๊ธฐ (2) ๋ณธ๋ฌธ

๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ตœ๋Œ“๊ฐ’ ๋งŒ๋“ค๊ธฐ (2)

Eun_zii 2024. 3. 15. 15:03

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0

- ์ตœ๋Œ“๊ฐ’ ๋งŒ๋“ค๊ธฐ (2)

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

  • ์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์˜ ์›์†Œ ์ค‘ ๋‘ ๊ฐœ๋ฅผ ๊ณฑํ•ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ“๊ฐ’์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • -10,000 ≤ numbers์˜ ์›์†Œ ≤ 10,000
  • 2 ≤ numbers ์˜ ๊ธธ์ด ≤ 100

 

๐Ÿ’š ํ’€์ด

function solution(numbers) {
    const sort = numbers.sort((a,b)=> a - b)
    return Math.max(numbers[0]*numbers[1], numbers[numbers.length-1]*numbers[numbers.length-2]);  
}

 

โœ… 

๋ฐฐ์—ด numbers๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ•ด์ค€ํ›„ Math.max( ) ํ•จ์ˆ˜๋กœ

๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ * ์ฒซ๋ฒˆ์งธ , ๋งˆ์ง€๋ง‰ * ๋งˆ์ง€๋ง‰ ์ „ ๋ฒˆ์งธ ์ค‘ ์ตœ๋Œ€๊ฐ’์„ return ํ•ด์ค€๋‹ค.

(๋ฐฐ์—ด์— ์Œ์ˆ˜๋„์žˆ๊ธฐ ๋•Œ๋ฌธ์— sortํ•ด์ฃผ์—ˆ์„๋•Œ ๋งˆ์ง€๋ง‰ ๊ฐ’๋“ค๋งŒ ๊ณฑํ•œ๋‹ค๊ณ  ์ตœ๋Œ€๊ฐ’์€ ์•„๋‹ˆ์—ˆ๋‹ค ,, )

728x90