๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[๋ฐฑ์ค€/Node.js] 10818๋ฒˆ ํ’€์ด || JavaScript

728x90
let input = require('fs').readFileSync('/dev/stdin').toString().trim().split("\n");

let testArray = input[1].split(" ").map(i => Number(i));

console.log(Math.min(...testArray), Math.max(...testArray));

 

๐ŸŽ‰ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ฐฐ์—ด์—์„œ ๊ฐ’๋งŒ ๊บผ๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ฉด ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐ŸŽ‰ ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๊ธฐ : Math.max(1, 2, 3, 4)

๐ŸŽ‰ ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๊ธฐ : Math.min(5, 2, 5, 7)

=> ๋‘˜๋‹ค ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’๋“ค ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•ด์„œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋“ค์ด๋‹ค. 

 

 

+ ๋ฐฐ์—ด์„ ๊ทธ๋Œ€๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

console.log(Math.min.apply(null, testArray), Math.max.apply(null, testArray));

 

๐ŸŽ‰Math.max.apply(null, ๋ฐฐ์—ด)

null ์—๋Š” ์›๋ž˜ Math.max() ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  this ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•˜์ง€๋งŒ,

์ฃผ์–ด์ง„ ๋ฐฐ์—ด๋งŒ ๊ฐ€์ง€๊ณ  ๋ฒ”์œ„๋ฅผ ๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— null ์„ ์ „๋‹ฌํ•ด์ค€๋‹ค. 

 

 

728x90
๋ฐ˜์‘ํ˜•