1. ๋ฌธ์ œ

OX๋ฌธ์ œ ์‹œํ—˜์—์„œ ์—ฐ์†์ ์œผ๋กœ ๋‹ต์„ ๋งžํžˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์‚ฐ์ ์„ ์ฃผ๊ธฐ๋กœ ํ–ˆ๋‹ค. ์•ž์˜ ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ต์„ ํ‹€๋ฆฌ๋‹ค๊ฐ€ ๋‹ต์ด ๋งž๋Š” ์ฒ˜์Œ ๋ฌธ์ œ๋Š” 1์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. ๋˜ํ•œ, ์—ฐ์†์œผ๋กœ ๋ฌธ์ œ์˜ ๋‹ต์ด ๋งž๋Š” ๊ฒฝ์šฐ์—์„œ ๋‘ ๋ฒˆ์งธ ๋ฌธ์ œ๋Š” 2์ , ์„ธ ๋ฒˆ์งธ ๋ฌธ์ œ๋Š” 3์ , ..., K๋ฒˆ์งธ ๋ฌธ์ œ๋Š” K์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. ํ‹€๋ฆฐ ๋ฌธ์ œ๋Š” 0์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. ๋‹ต์ด ๋งž์€ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” 1๋กœ ํ‘œ์‹œํ•˜๊ณ , ํ‹€๋ฆฐ ๊ฒฝ์šฐ์—๋Š” 0์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค. ์ฑ„์  ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ ์˜ˆ์ œ | 1 0 1 1 1 0 0 1 1 0

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

 

 

2. ๋ฌธ์ œํ’€์ด๊ณผ์ •

1. ๋ฐฐ์—ด ํƒ์ƒ‰(for ๋ฐ˜๋ณต๋ฌธ) 

2. ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ์ ์ˆ˜๋ฅผ ๋ˆ„์ ํ•ด์ฃผ๋Š”๋ฐ, ๊ฐ€์‚ฐ์ ์„ ๊ฐ™์ด ๋”ํ•˜๋ฉด์„œ ๊ณ„์‚ฐํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

 

[์‹œ๋„ 1]

function solution(arr){
	let answer = 0;
    for(let i=0; i<arr.length; i++){
    	if(A[i] === 1){
        	answer += 1;
        } else answer += 0;
     }
     return answer;
}

let A = [1, 0, 1, 1, 1, 0, 0, 1, 1, 0];
console.log(solution(A));

์ฒ˜์Œ์— ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ๊ฐ€์‚ฐ์  ๊ณ„์‚ฐ์ด ์•ˆ๋˜์–ด์„œ 6์ด ๋‚˜์™”๋‹ค. 

๊ฐ€์‚ฐ์  ๊ณ„์‚ฐ์„ ํ•˜๋ ค๋ฉด ๋ญ”๊ฐ€ ์š”์†Œ๊ฐ€ ๋” ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ ๋ญ˜๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€

 

[์‹œ๋„ 2]

function solution(arr){
	let answer = 0;
    let count;
    for(let i=0; i<arr.length; i++){
    	if(A[i] === 1){
        	answer += 1;
            count = answer++;
        } else answer += 0;
     }
     return count;
}

let A = [1, 0, 1, 1, 1, 0, 0, 1, 1, 0];
console.log(solution(A));

count ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์ค˜์„œ answer+1๊ฐ’์ด count์— ๋ˆ„์ ๋˜๋„๋ก ํ–ˆ๋‹ค. 

๊ทธ๋ ‡์ง€๋งŒ ์•„์ง ๊ณ„์‚ฐ์ด ์ •ํ™•ํžˆ ๋˜์ง€ ์•Š์•˜๋‹ค. 

 

[์‹œ๋„ 3]

function solution(arr){
	let answer = 0;
    let count = 0;
    	for(let i=0; i<arr.length; i++){
        	if(arr[i] === 1){
            	count++; // count = count + 1
                answer+=count; // answer = answer + count // answer์— count๋ˆ„์ 
            } else count=0; // 0์ด ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ count ์ดˆ๊ธฐํ™” 
         }
    return answer;
}

let A = [1, 0, 1, 1, 1, 0, 0, 1, 1, 0];
console.log(solution(A));

answer์™€ count์— ๋ชจ๋‘ ๋ˆ„์ ์„ ํ•ด์ค˜์•ผํ•˜๋‹ˆ๊นŒ 0์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. 

count์— ๊ฐ’์„ ๋จผ์ € ๋”ํ•˜๊ณ , answer์— count๋ฅผ ๋”ํ•ด์„œ ๋ˆ„์ ํ•ด์ค€๋‹ค. 

0์ ์ผ ๊ฒฝ์šฐ๋Š” count๋ฅผ ๋‹ค์‹œ 0์œผ๋กœ ๋งŒ๋“ ๋‹ค. 

 

 

for of ๋ฌธ(๋ฐฐ์—ด์˜ ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ˜๋ณต)์„ ์จ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค! 

function solution(arr){
	let answer = 0;
    let count = 0;
    	for(x of arr){
        	if(x === 1){
            	count++; // count = count + 1
                answer+=count; // answer = answer + count
            } else count=0;
         }
    return answer;
}

let A = [1, 0, 1, 1, 1, 0, 0, 1, 1, 0];
console.log(solution(A));

1. ๋ฌธ์ œ

์ด N๋ฒˆ์˜ ๊ฒŒ์ž„์„ ํ•˜์—ฌ A๊ฐ€ ์ด๊ธฐ๋ฉด A๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , B๊ฐ€ ์ด๊ธฐ๋ฉด B๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๋น„๊ธธ ๊ฒฝ์šฐ์—๋Š” D๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋‘ ์‚ฌ๋žŒ์˜ ๊ฐ ํšŒ์˜ ๊ฐ€์œ„, ๋ฐ”์œ„, ๋ณด ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด

๊ฐ ํšŒ๋ฅผ ๋ˆ„๊ฐ€ ์ด๊ฒผ๋Š”์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

๊ฐ€์œ„, ๋ฐ”์œ„, ๋ณด์˜ ์ •๋ณด๋Š” 1:๊ฐ€์œ„, 2:๋ฐ”์œ„, 3:๋ณด๋กœ ์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

ํšŒ์ˆ˜ 1 2 3 4 5
A์ •๋ณด 2 3 4 1 3
B์ •๋ณด 1 1 2 2 3
์Šน์ž A B A B D

 

์ž…๋ ฅ์˜ˆ์ œ | 

A 2 3 3 1 3

B 1 1 2 2 3

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

 

 

 

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

for๋ฌธ์œผ๋กœ A์™€ B์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ,

์กฐ๊ฑด๋ฌธ์œผ๋กœ A๊ฐ€ ์ด๊ธฐ๋Š” ์ƒํ™ฉ์„ ๋งŒ๋“ค๊ณ  A๋ฅผ answer์— ๋„ฃ์–ด์ฃผ๊ณ , ๊ทธ ์™ธ ์ƒํ™ฉ์€ B๊ฐ€ answer์— ๋“ค์–ด๊ฐ€๋„๋ก ํ•œ๋‹ค. 

function solution(A, B){
	let answet = "";
    for(let i=0; i<A.length; i++){
		if(A[i] === B[i]) answer = "D";
        // ์—ฌ๊ธฐ์„œ ๋ถ€ํ„ฐ A๊ฐ€ ์ด๊ธฐ๋Š” ์ƒํ™ฉ ๊ธฐ์ค€์œผ๋กœ ์จ์ฃผ๊ธฐ
        else if (A[i] === 1 && B[i] === 3) answer += "A";
        else if (A[i] === 2 && B[i] === 1) answer += "A";
        else if (A[i] === 3 && B[i] === 2) answer += "A";
        else answer += "B";
     }
     return answer;
}



let A = [2, 3, 3, 1, 3];
let B = [1, 1, 2, 2, 3];
console.log(solution(A, B));

๋ถ„๋ฅ˜๋ฅผ ํ•ด์•ผํ•  ๋•Œ๋Š” ๊ธฐ์ค€์„ ์ž˜ ์žก์•„์•ผํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” A๊ฐ€ ์ด๊ธฐ๋Š” ๊ฒƒ์— ๊ธฐ์ค€์„ ๋‘๊ณ  ๊ทธ ์™ธ์—๋Š” ๋ชจ๋‘ B๊ฐ€ ์ถœ๋ ฅ๋˜๋„๋ก ํ–ˆ๋‹ค.

 

 

3. ์‚ฌ์šฉ๊ฐœ๋…

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž && ์‚ฌ์šฉํ•ด์„œ ์–‘์ชฝ ๋ชจ๋‘ ์ฐธ์ด์–ด์•ผ true๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. ํ•˜๋‚˜๋ผ๋„ ๊ฑฐ์ง“์ด๋ฉด else if / else๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

1. ๋ฌธ์ œ

์„ ์ƒ๋‹˜์ด N(1<=N<=1000)๋ช…์˜ ํ•™์ƒ์„ ์ผ๋ ฌ๋กœ ์„ธ์› ์Šต๋‹ˆ๋‹ค. ์ผ๋ ฌ๋กœ ์„œ ์žˆ๋Š” ํ•™์ƒ์˜ ํ‚ค๊ฐ€ ์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋งจ ์•ž์— ์„œ ์žˆ๋Š” ์„ ์ƒ๋‹˜์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ•™์ƒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (์•ž์— ์„œ ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค๋ณด๋‹ค ํฌ๋ฉด ๋ณด์ด๊ณ , ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋ณด์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)

 

์ž…๋ ฅ์˜ˆ์ œ | 130 135 148 140 145 150 150 153

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

 

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

๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ(for๋ฌธ) ์กฐ๊ฑด์— ์ถฉ์กฑํ•˜๋ฉด(if๋ฌธ) answer์— ๋ˆ„์ ํ•ด์„œ ๋ฆฌํ„ดํ•œ๋‹ค.

max ๋ฐ”๊พธ๋ฉด์„œ ๋น„๊ตํ•˜๋Š”๊ฑด ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๋Š” ๊ฑฐ๋ž‘ ๊ฐ™๋‹ค.

function solution(arr){
    let answer = 1;
    // ์ฒซ๋ฒˆ์งธ ์‚ฌ๋žŒ์€ ๋ฌด์กฐ๊ฑด ๋ณด์ด๋‹ˆ๊นŒ ์นด์šดํŒ… 1๋ถ€ํ„ฐ
    let max = arr[0]; // max๋Š” ์ฒซ๋ฒˆ์งธ ์‚ฌ๋žŒ์˜ ํ‚ค๋กœ ์ดˆ๊ธฐํ™”
    for(let i=1; i < arr.length; i++){
        if(arr[i] > max) {
            max = arr[i];
            answer++;
    } 
    return answer;
    }
}
let arr = [130, 135, 148, 140, 145, 150, 150, 153];
console.log(solution(arr));

์ฒ˜์Œ์— ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ ์ œ๋Œ€๋กœ ๊ฐ’์ด ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค. 

์กฐ๊ฑด arr[i]๊ฐ€ max๋ณด๋‹ค ํฌ๋ฉด answer++ ์นด์šดํŒ… ๋จผ์ €ํ•ด์ฃผ๊ณ  max๊ฐ’์„ ๋ฐ”๊ฟ”์ค˜์•ผํ•œ๋‹ค. 

function solution(arr){
    let answer = 1;
    // ์ฒซ๋ฒˆ์งธ ์‚ฌ๋žŒ์€ ๋ฌด์กฐ๊ฑด ๋ณด์ด๋‹ˆ๊นŒ ์นด์šดํŒ… 1๋ถ€ํ„ฐ
    let max = arr[0]; // max๋Š” ์ฒซ๋ฒˆ์งธ ์‚ฌ๋žŒ์˜ ํ‚ค๋กœ ์ดˆ๊ธฐํ™”
    for(let i=1; i < arr.length; i++){
        if(arr[i] > max) {
        	answer++;
            max = arr[i];
    } 
    return answer;
    }
}
let arr = [130, 135, 148, 140, 145, 150, 150, 153];
console.log(solution(arr));

1. ๋ฌธ์ œ

N(1<=N<=100)๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ์ž์‹ ์˜ ๋ฐ”๋กœ ์•ž ์ˆ˜๋ณด๋‹ค ํฐ ์ˆ˜๋งŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.(์ฒซ ๋ฒˆ์งธ ์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด ์ถœ๋ ฅํ•œ๋‹ค)

 

์ž…๋ ฅ์˜ˆ์ œ | 7 3 9 5 6 12

์ถœ๋ ฅ์˜ˆ์ œ | 7 9 6 12

 

 

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

1์ฐจ์› ๋ฐฐ์—ด ํƒ์ƒ‰ํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ

๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ(for๋ฌธ) ์กฐ๊ฑด์— ์ถฉ์กฑํ•˜๋Š” ๊ฐ’๋“ค๋งŒ(if๋ฌธ) answer์— ๋„ฃ์–ด์„œ ๋ฆฌํ„ดํ•œ๋‹ค. 

function solution(arr){
	let answet = [];
    answer.push(arr[0]); // ์ฒซ๋ฒˆ์งธ ์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด ์ถœ๋ ฅํ•ด์•ผํ•˜๋ฏ€๋กœ ๋ฏธ๋ฆฌ ์จ์ฃผ๊ณ 
    for(let i = 1; i < arr.length; i++){
    // 0๋ฒˆ์€ ์ž…๋ ฅํ•ด์คฌ์œผ๋‹ˆ๊นŒ 1๋ฒˆ๋ถ€ํ„ฐ ๋ˆ๋‹ค
    	if(arr[i] > arr[i-1]) answer.push(arr[i]);
        // ์ž์‹ ์˜ ๋ฐ”๋กœ ์•ž์ˆ˜๋ณด๋‹ค ์ปค์•ผํ•˜๋‹ˆ๊นŒ ์ž์‹ (i)์™€ ์ž์‹ ๋ฐ”๋กœ์•ž(i-1)์„ ๋น„๊ตํ•ด์„œ ์ž์‹ (i)๊ฐ€ ํฌ๋ฉด ์กฐ๊ฑด ์ถฉ์กฑ
    }
    return answer;
}

let arr = [7, 3, 9, 5, 6, 12];
console.log(solution(arr));
        
        

1์ฐจ์› ๋ฐฐ์—ด ํƒ์ƒ‰ํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ

 

1. ๋ฌธ์ œ

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

์ž…๋ ฅ์˜ˆ์ œ | 

good

time

good

time

student

์ถœ๋ ฅ์˜ˆ์ œ |

good

time

student

 

 

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

์ค‘๋ณต ๋ฌธ์ž ์ œ๊ฑฐ๊ฐ€ String.indexOf()๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด,

์ค‘๋ณต ๋‹จ์–ด ์ œ๊ฑฐ๋Š” Array.indexOf()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

function solution(str){
	let answer = [];
    for(let i =; i < str.length; i++){
    	if(str.indexOf(str[i] === i) answer += str[i];
    }
    return answer;
}

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

์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ๋งŒ ํ•ด์„œ ๊ฐ’์€ ์ œ๋Œ€๋กœ ๋‚˜์™”์ง€๋งŒ ๋ฐฐ์—ด๋กœ ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค.

Array.filter() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•ด์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋“ค๋งŒ ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค. 

filter() ๋ฉ”์†Œ๋“œ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋ฐ, ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ value, index, array ์ˆœ์„œ๋Œ€๋กœ ๋ฐ›๋Š”๋‹ค.

function solution(str){
	let answer;
    anwser = str.filter((value, i) => { 
    // filter์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ’, ์ธ๋ฑ์Šค, ๋ฐฐ์—ด ์ˆœ์„œ๋Œ€๋กœ ๋ฐ›๋Š”๋‹ค. ๋”ฐ๋กœ ๋ณ€์ˆ˜์ง€์ •์„ ์•ˆํ•ด์ค˜๋„ ๋œ๋‹ค. 
    	if(str.indexOf(value) === i) return value;
        // ๊ฐ’์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๊ฐ€ ์‹ค์ œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๊ณ  ๋งž์œผ๋ฉด ๊ฐ’ ๋ฆฌํ„ดํ•œ๋‹ค.
    });
    return answer;
}

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

 

 

3. ์‚ฌ์šฉํ•œ ๊ฐœ๋…

String.indexOf()

๋ฌธ์ž์—ด์—์„œ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

 

Array.indexOf()

๋ฐฐ์—ด์—์„œ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

 

Array.filter(๊ฐ’, ์ธ๋ฑ์Šค, ์›๋ž˜๋ฐฐ์—ด)

์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋งŒ ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

๋ฐฐ์—ด ๋‚ด ๊ฐ ์š”์†Œ์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ์—์„œ ๋ฆฌํ„ด๊ฐ’์ด true์ธ ์• ๋“ค๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋งŒ๋“ ๋‹ค. false์ด๋ฉด ๋ฒ„๋ฆฐ๋‹ค. 

// ํŒจํ„ด
let result = students.filter((student) => student.enrolled);
// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ฐฐ์—ด student์ค‘์— ๋“ฑ๋ก๋œ ์• ๋“ค๋งŒ ๋ฝ‘์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

1.๋ฌธ์ œ

์†Œ๋ฌธ์ž๋กœ ๋œ ํ•œ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ์ค‘๋ณต๋œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์ œ๊ฑฐ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋Š” ์›๋ž˜ ๋ฌธ์ž์—ด์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.

์ž…๋ ฅ์˜ˆ์ œ | ksekkset

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

 

 

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

์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž๋ฅผ ์ฐพ์œผ๋ ค๋ฉด, for๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ํƒ์ƒ‰ํ•œ๋‹ค. 

๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ํ•˜๋‚˜์”ฉ indexOf()๋กœ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋งŒ ๋ฐ˜ํ™˜ํ•ด์„œ anser์— ๋ฌธ์ž ๋ˆ„์ ํ•ด์ค€๋‹ค. 

function solution(str){
	let answer = "";
    for(let i=0; i<str.length; i++){
    	if(str.indexOf(str[i]) === 1) answer += str[i];
    }
    return answer;
}

console.log(solution("ksekkset"));

"keskket"๋ฅผ indexOf(str[i]) ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•˜๋ฉด ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•œ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋‹ˆ๊นŒ

๋ฐ˜ํ™˜๋œ ์ธ๋ฑ์Šค

k/0

e/1

s/2

k/0

k/0

e/1

s/2

t/7

์›๋ž˜ ์ธ๋ฑ์Šค

k/0

e/1

s/2

k/3

k/4

e/5

s/6

t/7

 

 

3. ์‚ฌ์šฉํ•œ ๊ฐœ๋…

String.indexOf(์ธ๋ฑ์Šค)

๋ฌธ์ž์—ด์—์„œ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ๋ฉ”์†Œ๋“œ

 

String.indexOf()๋ฅผ ์ด์šฉํ•ด์„œ ์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€  ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. 

function solution(str){
	let answer = 0;
    let position = str.indexOf('k');
    // k์˜ ์ฒซ๋ฒˆ์งธ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค
	while(position !== -1){
    	answer++; // 0์— ๋ˆ„์ ํ•ด์„œ ์นด์šดํŒ…ํ•ด์ค€๋‹ค
        position - s.indexOf('k', positon+1);
    }
    return answer;
}

console.log(solution("ksekkset"));

1.๋ฌธ์ œ

์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด(๋ฌธ์ž์—ด)๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๊ทธ ๋‹จ์–ด์˜ ๊ฐ€์šด๋ฐ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜์ผ ๊ฒฝ์šฐ ๊ฐ€์šด๋ฐ 2๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ž…๋ ฅ์˜ˆ์ œ | study

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

์ž…๋ ฅ์˜ˆ์ œ | good

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

 

 

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

1. ๋ฌธ์ž์—ด์˜ ๊ฐ€์šด๋ฐ๋ฅผ ์ฐพ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

2. ๊ฐ€์šด๋ฐ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ, ํ™€์ˆ˜์ด๋ฉด ๊ฐ€์šด๋ฐ ๊ธ€์ž๋งŒ ์ง์ˆ˜์ด๋ฉด ๊ฐ€์šด๋ฐ 2๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. (์กฐ๊ฑด๋ฌธ์ด์šฉ)

function solution(str) {
	let answer;
    let mid = Math.floor(str.length/2);
    if(str.length % 2 == 1){
    	answer = str.substring(mid, mid+1);
        } else answer = str.substring(mid-1, mid+1);
    return answer;
}

console.log(solution("study"));

 

๊ฐ€์šด๋ฐ ์ˆซ์ž๋ฅผ ๋ณ€์ˆ˜ mid ์ฐพ์•„์ค€๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๊ตฌํ•ด์„œ 2๋กœ ๋‚˜๋ˆ„๊ณ  ์†Œ์ˆ˜์  ๋‚ด๋ฆผํ•˜๋Š” Math.floor()๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ substring์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ธ๋ฑ์Šค(์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ๊ณ„์‚ฐ)๋กœ ๋ฐ›๋Š”๋‹ค. 

if์กฐ๊ฑด๋ฌธ์œผ๋กœ ํ™€์ˆ˜์ด๋ฉด ๊ฐ€์šด๋ฐ ๊ธ€์ž๋งŒ, ์ง์ˆ˜์ด๋ฉด ๊ฐ€์šด๋ฐ ๋‘๊ธ€์ž๋งŒ ๋‚˜์˜ค๋„๋ก ์ธ๋ณ ์ง€์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

 

3. ์‚ฌ์šฉ๊ฐœ๋…

Math.floor()

์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ์ˆซ์ž์˜ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋‚ด๋ฆผํ•œ ์ •์ˆ˜ ๋ฐ˜ํ™˜

 

String.substring(์‹œ์ž‘์ธ๋ฑ์Šค, ๋งˆ์ง€๋ง‰์ธ๋ฑ์Šค)

๊ธฐ์กด ๋ฌธ์ž์—ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

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