FE

[JAVASCRIPT] - ์ •์ˆ˜๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

<zinny/> 2022. 1. 20. 10:39
728x90

๐Ÿคซ ์ •์ˆ˜(์ˆซ์ž)๋ฅผ ์ •๋ ฌํ•˜๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•! 

5 ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์น˜๋ฉด ๊ฐ€๋Šฅ

1. ์ •์ˆ˜(์ˆซ์ž)๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค

2. ๋ณ€ํ™˜ํ•œ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ฐ”๊พผ๋‹ค

3. ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค

4. ๋ฐฐ์—ด์„ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค

5. ์ด๋ชจ๋“  ๊ฒƒ์„ ๋‹ค์‹œ ์ •์ˆ˜(์ˆซ์ž)๋กœ ๋ฐ”๊พผ๋‹ค!

 

let n = 118372 //๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ณ  ์‹ถ์Œ

1. let a = n.toString()				//์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ
2. let b = a.split('')				//๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ
3. let c = b.sort((a,b) =>b-a)			//๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๊ณ 
4. let d = c.join('')				//๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด๋กœ
5. let e = parsInt(d)				//๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ
728x90