์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 12951] JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ (Javascript)

์€์ง„ 2021. 6. 24. 17:02

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 12951] JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ (Javascript)
๋ฌธ์ œ ์„ค๋ช…
JadenCase๋ž€ ๋ชจ๋“  ๋‹จ์–ด์˜ ์ฒซ ๋ฌธ์ž๊ฐ€ ๋Œ€๋ฌธ์ž์ด๊ณ , ๊ทธ ์™ธ์˜ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, s๋ฅผ JadenCase๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด
s๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
s๋Š” ์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ๋ฌธ์ž(" ")๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฒซ ๋ฌธ์ž๊ฐ€ ์˜๋ฌธ์ด ์•„๋‹๋•Œ์—๋Š” ์ด์–ด์ง€๋Š” ์˜๋ฌธ์€ ์†Œ๋ฌธ์ž๋กœ ์”๋‹ˆ๋‹ค. ( ์ฒซ๋ฒˆ์งธ ์ž…์ถœ๋ ฅ ์˜ˆ ์ฐธ๊ณ  )
์ž…์ถœ๋ ฅ ์˜ˆ
s return
"3people unFollowed me" "3people Unfollowed Me"
"for the last week" "For The Last Week"

 

โœ๏ธIdea Sketch

2021-06-24

1. ๋ฌธ์ž์—ด ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ split() --> arr ์ƒ์„ฑ
2. ๊ฐ arr ์›์†Œ์˜ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž๋Š” str.toUpperCase()
3. str.toUpperCase() ๋Œ€๋ฌธ์ž ๋ณ€ํ™˜
const sentence = 'The quick brown fox jumps over the lazy dog.';

console.log(sentence.toUpperCase());  // output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
4. ์ฒซ ๋ฌธ์ž๊ฐ€ ์˜๋ฌธ์ด ์•„๋‹๋•Œ์—๋Š” ์ด์–ด์ง€๋Š” ์˜๋ฌธ์€ ์†Œ๋ฌธ์ž๋กœ ์”๋‹ˆ๋‹ค.
  • str.toUpperCase()๋Š” str์— ์˜๋ฌธ์ด ์•„๋‹Œ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ, ๊ทธ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ดํ•จ
4. arr.map() ์‚ฌ์šฉ
  • ์•„๋ž˜์ฒ˜๋Ÿผ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ, ๋Œ€๋ฌธ์ž ์ „ํ™˜์ด ์ ์šฉ๋˜์งˆ ์•Š์Œ
arr.map(v => {
  v[0] = v[0].toUpperCase();
  return v;
})
5. arr.splice() ๋˜๋Š” arr.slice() ์‚ฌ์šฉํ•˜์—ฌ return
  • ๋ฌธ์ž์—ด ๋ณต์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ arr.slice() ์‚ฌ์šฉ
  • str.toLowerCase() ์‚ฌ์šฉ
6. array.splice(index, ์ œ๊ฑฐํ•  ์š”์†Œ์˜ ๊ฐœ์ˆ˜, ์ถ”๊ฐ€ํ•  ์š”์†Œ)
  • ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์‚ญ์ œ/๊ต์ฒด/์ถ”๊ฐ€
  • ์›๋ณธ์ด ๋ฐ”๋€œ
  • ๋ฐฐ์—ด ๋Œ€์ƒ
const months = ['Jan', 'March', 'April', 'June'];

months.splice(1, 0, 'Feb');  // index 1์— 'Feb' ์ถ”๊ฐ€ (0๊ฐœ ์ œ๊ฑฐ)
console.log(months);  // output: ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');  // index 4๋ฅผ 'May'๋กœ ๋Œ€์ฒด (1๊ฐœ "June" ์ œ๊ฑฐ)
console.log(months);  // output: ["Jan", "Feb", "March", "April", "May"]
7. array.slice(begin, end)
  • ๋ฐฐ์—ด ์ธ๋ฑ์Šค begin๋ถ€ํ„ฐ end๊นŒ์ง€ ์–•์€ ๋ณต์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜
  • ๋ณต์‚ฌ๋‹ˆ๊นŒ ์›๋ณธ์€ ๋ฐ”๋€Œ์ง€ ์•Š์Œ
  • end ๋ฐ”๋กœ ์ „๊นŒ์ง€ ๋ณต์‚ฌํ•˜๋ฉฐ, end๊ฐ€ ์—†์œผ๋ฉด ๋๊นŒ์ง€ ๋ณต์‚ฌ
  • ๋ฐฐ์—ด, ๋ฌธ์ž์—ด ๋Œ€์ƒ
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));  // output: ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));  // output: ["camel", "duck"] --> index 4์ธ 'elephant' ๋ฏธํฌํ•จ

console.log(animals.slice(1, 5));  // output: ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));  // output: ["duck", "elephant"]

console.log(animals.slice(2, -1));  // output: ["camel", "duck"] --> index -1์ธ 'elephant' ๋ฏธํฌํ•จ

 

2021-06-24

1. s๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๋ชจ๋“  ๊ณต๋ฐฑ์„ ๋ณ€ํ™˜๋œ ๋ฌธ์ž์— ๊ผญ ๋ฐ˜์˜ ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!!
  • ์•„๋ž˜์™€ ๊ฐ™์ด ๊ณต๋ฐฑ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋„ ์กด์žฌ
const str = "  3people   unFollowed   me"

let arr = str.split(' ');
console.log(arr)  // output ('' ์ž˜๋ณด๋ฉด ๊ณต๋ฐฑ์—†์Œ): [ '', '', '3people', '', '', 'unFollowed', '', '', 'me' ]

arr = arr.join(' ')
console.log(arr)  // output (str๊ณผ ์ผ์น˜): "  3people   unFollowed   me" 
2. ๊ณต๋ฐฑ์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๋Š” ์กฐ๊ฑด๋ฌธ ์ถ”๊ฐ€

 

โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-24 ์ •ํ™•์„ฑ 43.8์ 

function solution(s) {
  return s.split(' ').map(v => {
    return v[0].toUpperCase() + v.slice(1).toLowerCase();
  }).join(' ');
}

2021-06-24 ์ •ํ™•์„ฑ ํ†ต๊ณผ ๋ฌด๋ ค +13์ 

function solution(s) {
  return s.split(' ').map(v => {
    if (v === '') return v;
    return v[0].toUpperCase() + v.slice(1).toLowerCase();
  }).join(' ');
}

 

โœ๏ธ๋ช…๋‹ต

function solution(s) {
    return s.split(" ").map(v => v.charAt(0).toUpperCase() + v.substring(1).toLowerCase()).join(" ");
}

 

1. str.charAt(index) : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•˜๋Š” ๋‹จ์ผ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜
const str = 'The quick brown fox';

const index = 4;

console.log(str.charAt(index));  // output: "q"
2. str.substring(begin, end) : ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋กœ๋ถ€ํ„ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค ์ง์ „๊นŒ์ง€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜
  • end๊ฐ€ ์—†์œผ๋ฉด ๋ฌธ์ž์—ด ๋๊นŒ์ง€ ๋ฐ˜ํ™˜
const str = 'Mozilla';

console.log(str.substring(1, 3));  // output: "oz"

console.log(str.substring(2));  // output: "zilla"