1. ๋ฌธ์ œ

N๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ๊ทธ ์ค‘ ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

์ž…๋ ฅ์˜ˆ์ œ | teacher, time, student, beautiful, good

์ถœ๋ ฅ์˜ˆ์ œ | beautiful

 

 

 

2. ๋ฌธ์ œํ’€์ด

function solution(s){
	let answer;
    let max = Number.MIN_SAFE_INTEGER;
    for(let x of s){
    	if(x.length > max){
        	max = x.length;
            answer = x;
        }
    }
    return answer;
}

let str = ["teacher", "time", "student", "beautiful", "good"];
console.log(solution(str));

๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ฐพ์•„์•ผ ํ•˜๋‹ˆ๊นŒ max๋ฅผ ์ •์ˆ˜ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์„œ ์ดˆ๊ธฐํ™”๋ฅผ ์‹œ์ผœ์ค€ ๋‹ค์Œ,

for of ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ length๊ฐ€ max๋ณด๋‹ค ํฌ๋ฉด max์— ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ‘ผ๋‹ค. 

 

์ตœ๋Œ€๊ฐ’ ์ฐพ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— max๋„ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค€๋‹ค. max๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. 

// x.length๊ฐ€ max๋ณด๋‹ค ํฌ๋ฉด max๋ฅผ ๊ทธ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. ์šฐํ•ญ์—์„œ ์ขŒํ•ญ์œผ๋กœ ์—ฐ์‚ฐ๋จ

+ Recent posts