Programmers ํด์: ์์ฃผํ์ง ๋ชปํ ์ ์
Coding Test
2021. 8. 22. 18:02
โ ์๋ก ๋ฐฐ์ด ์
๋ฐฐ์ด์ find ํจ์
๋ฐฐ์ด ๊ด๋ จํ ๋ฉ์๋ ๋ฐฐ์ธ ๋ ์ผํ ๋ดค๋ ๊ฒ ๊ฐ์๋ฐ ์ ๋๋ก ๊ธฐ์ต์ด ์๋ ๋ค์ ํ๋ฒ ์ ๋๋ก ์ฐพ์๋ณด์๋ค.
for๋ฌธ์ผ๋ก๋ ์ฌ์ฉํด์ ํ ์ ์๊ฒ ์ง๋ง! find ํจ์ ์ฌ์ฉํด๋ณด๊ณ ์ถ์๋น!
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Array.prototype.find() - JavaScript | MDN
find() ๋ฉ์๋๋ ์ฃผ์ด์ง ํ๋ณ ํจ์๋ฅผ ๋ง์กฑํ๋ ์ฒซ ๋ฒ์งธ ์์์ ๊ฐ์ ๋ฐํํฉ๋๋ค. ๊ทธ๋ฐ ์์๊ฐ ์๋ค๋ฉด undefined๋ฅผ ๋ฐํํฉ๋๋ค.
developer.mozilla.org
< for๋ฌธ ์ฌ์ฉ >
function solution(participant, completion) {
participant.sort();
completion.sort();
for (let i = 0; i<participant.length; i++) {
if(participant[i] !== completion[i]){
return participant[i]
}
}
}
< find method ์ฌ์ฉ >
function solution(participant, completion) {
participant.sort();
completion.sort();
let answer = participant.find((el, index) => el !== completion[index]);
return answer;
}
'Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
LeetCode - Two Sum (0) | 2021.11.08 |
---|---|
Programmers: ๋ ๊ฐ ๋ฝ์์ ๋ํ๊ธฐ (0) | 2021.08.22 |
Programmers: ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ ๊ฒ์ (0) | 2021.08.22 |
Programmers ์์ ํ์: ๋ชจ์๊ณ ์ฌ (0) | 2021.08.15 |
Programmers ์ ๋ ฌ: K๋ฒ์งธ์ (0) | 2021.08.15 |