javascript
[์๋ฐ์คํฌ๋ฆฝํธ] ๋ฌธ์์ด ์ค ๋ช๊ฐ๊ฐ ๋ค๋ฅธ ๋ฌธ์์ธ์ง ๊ฐ์ ์ธ๊ธฐ | ๋ฌธ์์ด ๋น๊ต
ruby_s
2022. 1. 25. 14:56
728x90
๋ฐ์ํ
SMALL
reduce ํ์ฉํ๊ธฐ
const adj = [];
const difIdx = [...el].reduce((acc, curr, idx) => curr !== word[idx] ? acc += 1 : acc, 0);
if(difIdx === 1) adj.push(el);
๊ฐ๋จํ๊ฒ for๋ฌธ์ ๋๋ ค์ ์นด์ดํธ๋ฅผ ์ฆ๊ฐ์ํค๋ฉด ๋น๊ตํ๊ณ ์ ํ๋ ๋ฌธ์์ด๊ณผ ๋ช๊ฐ์ ๋ฌธ์๊ฐ ๋ค๋ฅธ์ง ์ ์ ์์ง๋ง, reduce๋ฅผ ํ์ฉํ ์๋ ์๋ค!
์์ ์์์์ ๋ณด๋ฉด, difIdx ๋ ๋ค๋ฅธ ๋ฌธ์์ ๊ฐ์๊ฐ ์ ์ฅ๋ ๊ฒ์ด๋ค.
์๋ฅผ ๋ค์ด, word = 'apple', el = 'appel'์ผ ๊ฒฝ์ฐ
๋ค์ 2๋ฌธ์๊ฐ ๋ค๋ฅด๋ฏ๋ก difIdx = 2๊ฐ ๋๋ค.
728x90
๋ฐ์ํ
LIST