1. ๋ฌธ์ œ

๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ฐ™์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

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

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

 

 

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

answer๋Š” ๋นˆ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด ๋†“๊ณ , ์ธ์ˆ˜๋กœ ๋ฐ›์€ ๋ฌธ์ž์—ด์„ for of๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ if-else๋ฌธ์œผ๋กœ ๋Œ€๋ฌธ์ž์ด๋ฉด ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๋ˆ„์ ํ•˜๊ณ , ์†Œ๋ฌธ์ž๋ฉด ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๋ˆ„์ ํ•œ ๋’ค answer ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

function solution(str){
	for(let x of str){
		if(x === x.toUpperCase()){
    		answer += x.toLowerCase();
    	} else answer += x.toUpperCase();
    }
    return answer;
}

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

 

 

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

- String.toUpperCase() 

๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜

- String.toLowerCase()

๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜

1. ๋ฌธ์ œ

๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ฐ™์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋Œ€๋ฌธ์ž๋กœ ๋ชจ๋‘ ํ†ต์ผํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

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

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

 

 

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

answer๋ฅผ ๋นˆ๋ฌธ์ž์—ด๋กœ ๋น„์›Œ๋†“๊ณ  ์ธ์ˆ˜๋กœ ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ answer ๋ฌธ์ž์—ด์— ํ•˜๋‚˜์”ฉ ๋ˆ„์ ํ•ด์ค€๋‹ค.

function solution(str) {
	let answer = "";
    for(let x of str){
    	answer += x.toUpperCase();
    }
    return answer;
}

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

1. ๋ฌธ์ œ

ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ๋ฌธ์ž์—ด์— ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

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

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

 

 

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

for of๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ํƒ์ƒ‰ํ•ด์„œ ๋Œ€๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ๊ฒฝ์šฐ์—๋งŒ answer๊ฐ’ ๋ˆ„์ ์‹œ์ผœ์„œ ์ถœ๋ ฅํ•˜๊ธฐ

function solution(str) {
	let answer = 0;
    for(let x of str) {
    	if(x === x.toUpperCase()) answer++;
    }
    return answer;
}

console.log(solution("KoreaTimeGood");

 

1. ๋ฌธ์ œ

ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๊ณ , ํŠน์ • ๋ฌธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ํŠน์ •๋ฌธ์ž๊ฐ€ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์— ๋ช‡๊ฐœ ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)

์ž…๋ ฅ์„ค๋ช… | ์ฒซ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์— ๋ฌธ์ž๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ์„ค๋ช… | ์ฒซ ์ค„์— ํ•ด๋‹น ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…๋ ฅ์˜ˆ์ œ | COMPUTERPROGRAMMING / R

 

 

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

๋ฌธ์ž์—ด ์•ˆ์— ํŠน์ • ๋‹จ์–ด๊ฐ€ ๋ช‡๊ฐœ ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด์•ผํ•œ๋‹ค.

๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ๋Œ๋ฉด์„œ ํ•ด๋‹น ๋‹จ์–ด ์žˆ์œผ๋ฉด answer๊ฐ’ ๋ˆ„์  ์ฆ๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์จ์ฃผ์ž.

1. answer์— ๋ˆ„์ countingํ•˜์—ฌ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•˜๋ฏ€๋กœ -> let answer = 0 ์ด๋ผ๊ณ  ์จ์ค€๋‹ค.

2. for of ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์˜ ๋ฌธ์žํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ ํƒ์ƒ‰ -> ์กฐ๊ฑด๋ฌธ์œผ๋กœ x๊ฐ€ ์ธ์ˆ˜์™€ ๊ฐ™์œผ๋ฉด answer๋ฅผ ์ฆ๊ฐ์‹œํ‚จ๋‹ค.

function solution(word) {
	let answer = 0;
    for (let x of str){
    	if(x === word) answer++;
    }
    return answer;
}

let str = "COMPUTERPROGRAMMING";
console.log(solution('R');

 

 

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

์ž…๋ ฅ๋œ ์˜ˆ์ œ๊ฐ€ ์†Œ๋ฌธ์ž ์˜€์ง€๋งŒ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž์ธ ๋ฌธ์ž์—ด์—์„œ ์ฐพ์•„์•ผํ•œ๋‹ค๋ฉด?

๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” toUpperCase ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

function solution(word) {
	let answer = 0;
    for (let x of str){
    	if(x === word.toUpperCase()) answer++;
    }
    return answer;
}

let str = "COMPUTERPROGRAMMING";
console.log(solution('r');

 

2. ๋ฌธ์ œ ํ’€์ด (3) - ์ ์šฉ ๋ฌธ์ œ

๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ split (๋ฌธ์ž์—ด์„ ๋ฐฐ์—ดํ™” ์‹œ์ผœ์„œ ๋ฐ˜ํ™˜) 

R์„ ๊ธฐ์ (R์ด ๊ตฌ๋ถ„์ž) ์œผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฐฐ์—ดํ™” ์‹œ์ผœ์„œ ๋ฐ˜ํ™˜

 

answer ์— ๋ฐ”๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋„๋ก ํ•œ๋‹ค. 

function solution(s, t){
	let answer = s.split(t).length;
    return answer-1;
 }
 let str = "COMPUTERPROGRAMMING";
 console.log(solution(str, 'R'));

 

R์„ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜๋ˆ„๊ณ  ๋ฐฐ์—ด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ -1ํ•ด์ค˜์„œ ์ฐพ์„์ˆ˜๋„ ์žˆ๋‹ค.

 

 

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

String.toUpperCase() 

// ์˜ˆ์‹œ
const str = "Hello World";

str.toUpperCase(); // "HELLO WORLD"

 

1. ๋ฌธ์ œ

๋Œ€๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ์˜์–ด๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๋‹จ์–ด์— ํฌํ•จ๋œ ‘A'๋ฅผ ๋ชจ๋‘ ’#‘์œผ๋กœ ๋ฐ”๊พธ์–ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

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

์ถœ๋ ฅ ์˜ˆ์ œ | B#N#N#

 

 

 

๊ฐ„๋‹จํ•œ ๋ฌธ์ž์—ด ํƒ์ƒ‰ ๋ฌธ์ œ

 

 

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

1. answer๋Š” ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅ๋œ๋‹ค. 

2. for๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด ํƒ์ƒ‰ ๊ฐ€๋Šฅํ•˜๋‹ค -> for of๋ฌธ ์‚ฌ์šฉ -> if๋ฌธ์œผ๋กœ ๋ณ€์ˆ˜ x๊ฐ€ ๋Œ€๋ฌธ์ž A์ธ์ง€ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•œ๋‹ค. 

 function solution(word) {
            let answer = ""; // answer๋กœ ์ถœ๋ ฅ๋  ๊ฐ’์€ ๋ฌธ์ž์—ด์ด๋‹ˆ๊นŒ
            for(let x of word){
                if(x === 'A') answer += '#';
                else answer += x;
            }
            return answer;
        }

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

 

๋ณ€์ˆ˜๋กœ ์ž‘์„ฑํ•œ answer๋Š” ๋นˆ ๋ฌธ์ž์—ด์ด๋‹ค.

for ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•ด์ค€๋‹ค. 

์ธ์ˆ˜๋กœ ๋ฐ›์€ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ํ™•์ธํ•˜๋ฉด์„œ (for ๋ฌธ์ž of ๋ฌธ์ž์—ด) ๋ฌธ์ž์—ด์„ ์ฑ„์›Œ๊ฐ€๋Š” ๋ฐฉ์‹

x === 'A'๊ฐ€ true์ด๋ฉด answer = answer + '#' ์ถœ๋ ฅ / ๋ˆ„์  / x === 'A'๊ฐ€ false์ด๋ฉด answer = answer + x 

 

*ํฐ๋”ฐ์˜ดํ‘œ๋Š” ๋ฌธ์ž์—ด์ผ๊ฒฐ์šฐ, ์ž‘์€ ๋”ฐ์˜ดํ‘œ๋Š” ๋ฌธ์ž ํ•˜๋‚˜ ์“ฐ์ผ ๋•Œ ์‚ฌ์šฉ

 

 

 

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

String.replace ๋ฉ”์„œ๋“œ / ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉ

A๋ผ๋Š” ๋Œ€๋ฌธ์ž๋ฅผ ์ฐพ์•„๋ผ / ๋ฐ”๊พธ๋Š” ๋ฌธ์ž๋Š” #์œผ๋กœ ๋ฐ”๊ฟ”๋ผ -> (/A/, '#')

๋ฌธ์ž์—ด์€ ์–‰์€ ๋ณต์‚ฌ ๋˜์ง€์•Š๋Š”๋‹ค. ๋ฐฐ์—ด์€ ์ฃผ์†Œ ์ฐธ์กฐ๋˜๋Š”๋ฐ ๋ฌธ์ž์—ด์€ ๊ฐ’์ด ๋ณต์‚ฌ๋˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด๋‹ค.

function solution(word){
	let answer = word; //์–‰์€ ๋ณต์‚ฌ but,๋ฌธ์ž์—ด์€ ์ฃผ์†Œ ์ฐธ์กฐ๋˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๊ฐ’์ด ๋ณต์‚ฌ๋˜์–ด ๋ฐ”๋€Œ์–ด๋ฒ„๋ฆฐ๋‹ค.
	answer = answer.replace(/A/g, '#'); 
	// ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉ word๋ผ๋Š” string์—์„œ A๋ฅผ ์ฐพ์•„์„œ string #๋กœ ๋ฐ”๊ฟ”๋ผ
	// ๊ทธ๋ƒฅ ๋Œ€๋ฌธ์ž A๋งŒ ์ ์œผ๋ฉด ํ•œ๋ฒˆ๋งŒ ์ฐพ๋Š”๋‹ค. ์ „์—ญ์—์„œ ๋ชจ๋‘ ์ฐพ์•„์•ผํ•˜๋‹ˆ๊นŒ global ์˜๋ฏธ์˜ g๋ฅผ ํ•จ๊ป˜ ์จ์ค€๋‹ค.
	return answer;
}

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

 

1. ๋ฌธ์ œ

์ผ๊ณผ๋ฅผ ๋งˆ์น˜๊ณ  ๋Œ์•„์˜จ ๋‚œ์Ÿ์ด๊ฐ€ ์ผ๊ณฑ๋ช…์ด ์•„๋‹ˆ๋ผ ์•„ํ™‰๋ช… ์ด์—ˆ๋‹ค. ๋ฐฑ์„ค๊ณต๋ถ€๋Š” ์ผ๊ณฑ ๋‚œ์Ÿ์ด์˜ ํ‚ค์˜ ํ•ฉ์ด 100์ด ๋จ์„ ์•Œ๊ณ ์žˆ์—ˆ๋‹ค. ์•„ํ™‰ ๋‚œ์Ÿ์ด์˜ ํ‚ค๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฐฑ์„ค๊ณต์ฃผ๋ฅผ ๋„์™€ ์ผ๊ณฑ ๋‚œ์Ÿ์ด๋ฅผ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ์˜ˆ์ œ | 20 7 23 19 10 15 25 8 13

 

 

2. ๋‚ด๊ฐ€ ํ‘ผ ๋ฐฉ๋ฒ•

9๊ฐœ ๋‹ค ๋”ํ•œ ์ดํ•ฉ - 100 = n

2๊ฐœ ๋”ํ•œ๊ฒŒ n ์ด ๋‚˜์˜ค๋ฉด ๊ทธ ๋‘๊ฐœ๋ฅผ ๋นผ๋ฒ„๋ฆฌ๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.

๋‘๊ฐœ์”ฉ ๋”ํ•ด์„œ ๋งž๋Š”์ง€ ์•ˆ๋งž๋Š”์ง€ ๋น„๊ต๋ฅผ ํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ, ์ด์ค‘ for๋ฌธ์„ ๋Œ๋ฉด์„œ ์ „๋ถ€ ๋‹ค ํ•ด๋ด์•ผํ•œ๋‹ค.

 

 

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

1. 9๊ฐœ์˜ ์ดํ•ฉ์„ ๊ตฌํ•˜๊ณ  ๊ฐ€์งœ ๋‚œ์Ÿ์ด 2๊ฐœ๋ฅผ ๋”ํ•œ ๊ฐ’์„ ๋นผ์„œ 100์ด ๋งž์œผ๋ฉด

sum - (arr[i] + arr[j]) === 100

2. splice ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 2๊ฐœ ๊ฐ’ ์ œ๊ฑฐํ•ด๋ฒ„๋ฆฌ๊ณ  ์ถœ๋ ฅํ•œ๋‹ค. 

function solution(arr) {
            let answer = arr; // ์–‡์€ ๋ณต์‚ฌ : ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ๊ฐ™์€ ๋ฐฐ์—ด์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๊ฒƒ(์ฃผ์†Œ๋งŒ๋ณต์‚ฌ)
            let sum = arr.reduce((a,b)=> a+b,0);
            // 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 9๊ฐœ๋‹ˆ๊นŒ 8๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊นŒ์ง€ ๋Œ๊ฑฐ์•ผ. i๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘
            for(let i=0; i<8; i++){
                // j๋Š” i์˜ ๋’ค์—๊บผ๋ผ์„œ +1ํ•ด์ค€๋‹ค. j๋Š” 1๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ถ€ํ„ฐ ์‹œ์ž‘
                for(let j=i+1; j<9; j++){
                    if((sum-(arr[i]+arr[j])) === 100){
                        arr.splice(j, 1); // ์•ž์—๊บผ๋ถ€ํ„ฐ ์ง€์šฐ๋ฉด ์ธ๋ฑ์Šค๊ฐ€ ๋ฐ”๋€Œ์–ด๋ฒ„๋ ค์„œ ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ง€์šด๋‹ค
                        arr.splice(i, 1);
                
                    }
                }
            }
            return answer;
        }

        let arr = [20, 7, 23, 19, 10, 15, 25, 8, 13];
        console.log(solution(arr));

 

 

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

Array.reduce ๋ฉ”์†Œ๋“œ (๋ฐฐ์—ด์˜ ๋ˆ„์ ๊ฐ’, ์ดํ•ฉ ๊ตฌํ•˜๊ธฐ)

reduce ๋ฉ”์†Œ๋“œ๋Š” ์ž์‹ ์„ ํ˜ธ์ถœํ•œ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋Œ๋ฉด์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๊ตฌํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

// ๊ตฌ๋ฌธ ์ •๋ฆฌ
array.reduce(function(๋ˆ„์ ๊ฐ’, ํ˜„์žฌ๊ฐ’, ์ธ๋ฑ์Šค, ๋ฐฐ์—ด){ return ๋ˆ„์ ๊ฐ’ + ํ˜„์žฌ๊ฐ’ }, 0);
// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ
array.reduce((๋ˆ„์ ๊ฐ’, ํ˜„์žฌ๊ฐ’, ์ธ๋ฑ์Šค, ๋ฐฐ์—ด) => return ๊ฒฐ๊ณผ, ์ดˆ๊ธฐ๊ฐ’);

 

 

Array.splice ๋ฉ”์†Œ๋“œ (๋ฐฐ์—ด ๋‚ด ํŠน์ • ์ธ๋ฑ์Šค ์ œ๊ฑฐ)

์›๋ณธ ๋ฐฐ์—ด์˜ ์ค‘๊ฐ„์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ค‘๊ฐ„์— ์žˆ๋Š” ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

splice๋ฉ”์†Œ๋“œ๋Š” 3๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉฐ ์›๋ณธ ๋ฐฐ์—ด์„ ์ง์ ‘ ๋ณ€๊ฒฝํ•œ๋‹ค.

// ๊ตฌ๋ฌธ ์ •๋ฆฌ
๋ฐฐ์—ด.splice(์ธ๋ฑ์Šค, 1) // ์ธ๋ฑ์Šค์— ์žˆ๋Š” 1๊ฐœ ์š”์†Œ ์ œ๊ฑฐํ•œ๋‹ค




const arr = [1, 2, 3, 4];

const result = arr.splice(1, 2, 20, 30);
// ์›๋ณธ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค 1๋ถ€ํ„ฐ 2๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ์ž๋ฆฌ์— ์ƒˆ๋กœ์šด ์š”์†Œ 20, 30์„ ์‚ฝ์ž…ํ•œ๋‹ค. 

 

 

์ด์ค‘ for๋ฌธ ์˜ˆ์ œ๋ฅผ ๋” ํ’€๊ณ  ๊ณต๋ถ€๋ฅผ ํ•ด์•ผ๊ฒ ๋‹ค.

1. ๋ฌธ์ œ

์„œ์šธ์‹œ๋Š” 6์›” 1์ผ๋ถ€ํ„ฐ ๊ตํ†ต ํ˜ผ์žก์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์ž๋™์ฐจ 10๋ถ€์ œ๋ฅผ ์‹œํ–‰ํ•œ๋‹ค. ์ž๋™์ฐจ 10๋ถ€์ œ๋Š”์ž๋™์ฐจ ๋ฒˆํ˜ธ์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž์™€ ๋‚ ์งœ์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ํ•ด๋‹น ์ž๋™์ฐจ์˜ ์šดํ–‰์„ ๊ธˆ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ž๋™์ฐจ ๋ฒˆํ˜ธ์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ 7์ด๋ฉด 7์ผ, 17์ผ, 27์ผ์— ์šดํ–‰ํ•˜์ง€ ๋ชปํ•œ๋‹ค. ๋˜ํ•œ, ์ž๋™์ฐจ ๋ฒˆํ˜ธ์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ 0์ด๋ฉด 10์ผ, 20์ผ, 30์ผ์— ์šดํ–‰ํ•˜์ง€ ๋ชปํ•œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค์€ ์ผ์ผ ๊ฒฝ์ฐฐ๊ด€์ด ๋˜์–ด 10๋ถ€์ œ๋ฅผ ์œ„๋ฐ˜ํ•˜๋Š” ์ž๋™์ฐจ์˜ ๋Œ€์ˆ˜๋ฅผ ์„ธ๋Š” ๋ด‰์‚ฌํ™œ๋™์„ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋‚ ์งœ์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง€๊ณ  7๋Œ€์˜ ์ž๋™์ฐจ ๋ฒˆํ˜ธ์˜ ๋ ๋‘ ์ž๋ฆฌ ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์œ„๋ฐ˜ํ•˜๋Š” ์ž๋™์ฐจ์˜ ๋Œ€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ์„ค๋ช… | ์ฒซ ์ค„์—๋Š” ๋‚ ์งœ์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” 7๋Œ€์˜ ์ž๋™์ฐจ ๋ฒˆํ˜ธ์˜ ๋ ๋‘ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ์„ค๋ช… | ์ฃผ์–ด์ง„ ๋‚ ์งœ์™€ ์ž๋™์ฐจ์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ๋ณด๊ณ  10๋ถ€์ œ๋ฅผ ์œ„๋ฐ˜ํ•˜๋Š” ์ฐจ๋Ÿ‰์˜ ๋Œ€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ž…๋ ฅ์˜ˆ์ œ | 3 / 25 23 11 47 53 17 33

 

 

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

1. ๋์ž๋ฆฌ๊ฐ€ n์ธ ์• ๋“ค์„ ๋ฝ‘์•„์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ ๋‹ค.

2. ๋ฐฐ์—ด์˜ length๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 function solution(n, arr){
 	let newArr = [];
    for(let item of arr){
    	if(item % 10 === n) {
        	newArr.push(item);
        }
    }
    return newArr.length;
}

let arr = [25, 23, 11, 47, 53, 17, 33];
console.log(solution(3, arr));

*์–ด๋–ค ์ˆซ์ž์ด๋“ ์ง€  10์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๋‚˜๋จธ์ง€๊ฐ€ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ ์ˆซ์ž(์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž)๊ฐ€ ๋œ๋‹ค.

 

 

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

n์ด ๊ฐ™์œผ๋ฉด ์นด์šดํŒ…(์ฆ๊ฐ€์‹œ์ผœ์•ผํ•จ) ํ•ด์•ผ๋˜๋‹ˆ๊นŒ answer = 0

for๋ฌธ ๋Œ๋ฉด์„œ n๊ณผ ๊ฐ™์€์ง€ ๋ด์•ผํ•˜๋‹ˆ๊นŒ for of ๋ฌธ์„ ์‚ฌ์šฉํ•ด์ค€๋‹ค.

 function solution(n, arr){
 	let answer = 0; // ์œ„๋ฐ˜ํ•˜๋Š” ์ฐจ์˜ ์ˆ˜ ์ถœ๋ ฅํ•ด์•ผํ•ด์„œ 
    for(let item of arr){
    	if(item % 10 === n) answer++; // ์œ„๋ฐ˜ํ•˜๋Š” ์ฐจ๋Ÿ‰์ด๋ฉด ํ•œ๋Œ€์”ฉ ์ถ”๊ฐ€๊ฐ€ ๋˜๋Š” ๋ฐฉ์‹
    }
    return answer;
}

let arr = [25, 23, 11, 47, 53, 17, 33];
console.log(solution(3, arr));

 

7๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ด๋“ค ์ค‘ ํ™€์ˆ˜์ธ ์ž์—ฐ์ˆ˜๋“ค์„ ๋ชจ๋‘ ๊ณจ๋ผ ๊ทธ ํ•ฉ์„ ๊ตฌํ•˜๊ณ , ๊ณ ๋ฅธ ํ™€์ˆ˜๋“ค ์ค‘ ์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

์ž…๋ ฅ์˜ˆ์ œ | 12 77 38 41 53 92 85

์ถœ๋ ฅ์˜ˆ์ œ | 256, 41


[๋‚ด๊ฐ€ ํ’€์—ˆ๋˜ ๋ฐฉ๋ฒ•]

1. ํ™€์ˆ˜๋ฅผ ์ฐพ์•„์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ ๋‹ค.

2. ํ™€์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด ์†์—์„œ ์ด ํ•ฉ์„ ๊ตฌํ•œ๋‹ค. (forEach ๋ฉ”์†Œ๋“œ)

3. ํ™€์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด ์†์—์„œ ์ตœ์†Œ๊ฐ’ ์ฐพ๋Š”๋‹ค.(Math.min ๋ฉ”์†Œ๋“œ)

4. ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๊ฐ’ 2๊ฐœ๋ฅผ ๋„ฃ์–ด์„œ ์ถœ๋ ฅํ•œ๋‹ค. (Array.push ๋ฉ”์†Œ๋“œ)

 

function solution(arr){
	let oddNumber = []; // ํ™€์ˆ˜๋ฅผ ์ฐพ์•„์„œ ๊ฐ’ ์ €์žฅ
    let answer = []; // ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฐ์—ด
    let sum = 0; // ํ™€์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด ์š”์†Œ๋“ค์˜ ์ดํ•ฉ ๊ตฌํ•˜๊ธฐ
    for(let item of arr){
    	if(item % 2 !== 0) oddNumber.push(item);
        }
    let min = Math.min(...oddNumber);
    oddNumber.forEach((item)=>{
    	sum += item;
    })
    
    answer.push(sum);
    answer.push(min);
    
    return answer;
}

let arr = [12, 77, 38, 41, 53, 92, 85]
console.log(solution(arr));
    

 

 

for of๋ฌธ : ๋ฐฐ์—ด์˜ ๊ฐ’์„ 

 

[๋‹ค๋ฅธ ํ’€์ด๋ฐฉ๋ฒ•]

function solution(arr){
	let answer=[];
    let sum=0; min=Number.MAX_SAFE_INTEGER;
    for(let x of arr){
    	if(x%2===1){
        	sum+=x;
            if(x<min) min=x;
        }
    }
    answer.push(sum);
    answer.push(min);
    return answer;
    }

arr =[12, 77, 38, 41, 53, 92, 85];
console.log(solution(arr));

 

+ Recent posts