let's get IT with DAVINA ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]Lv0. ๋ชจ์Šค๋ถ€ํ˜ธ(1) ๋ณธ๋ฌธ

DEV_IN/๋ฌธ์ œํ’€์ด

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]Lv0. ๋ชจ์Šค๋ถ€ํ˜ธ(1)

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 3. 16. 23:15

โ“ ๋ฌธ์ œ ์„ค๋ช…

๋จธ์“ฑ์ด๋Š” ์นœ๊ตฌ์—๊ฒŒ ๋ชจ์Šค๋ถ€ํ˜ธ๋ฅผ ์ด์šฉํ•œ ํŽธ์ง€๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ์€ ์ฝ์„ ์ˆ˜ ์—†์–ด ์ด๋ฅผ ํ•ด๋…ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด letter๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, letter๋ฅผ ์˜์–ด ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.
๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
morse = { 
    '.-':'a','-...':'b','-.-.':'c','-..':'d','.':'e','..-.':'f',
    '--.':'g','....':'h','..':'i','.---':'j','-.-':'k','.-..':'l',
    '--':'m','-.':'n','---':'o','.--.':'p','--.-':'q','.-.':'r',
    '...':'s','-':'t','..-':'u','...-':'v','.--':'w','-..-':'x',
    '-.--':'y','--..':'z'
}โ€‹

๐Ÿšซ ์ œํ•œ์‚ฌํ•ญ

  • 1 ≤ letter์˜ ๊ธธ์ด ≤ 1,000
  • return๊ฐ’์€ ์†Œ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.
  • letter์˜ ๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • letter์— ๊ณต๋ฐฑ์€ ์—ฐ์†์œผ๋กœ ๋‘ ๊ฐœ ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํ•ด๋…ํ•  ์ˆ˜ ์—†๋Š” ํŽธ์ง€๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํŽธ์ง€์˜ ์‹œ์ž‘๊ณผ ๋์—๋Š” ๊ณต๋ฐฑ์ด ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ—๏ธ ์ž…์ถœ๋ ฅ ์˜ˆ

letter result
".... . .-.. .-.. ---" "hello"
".--. -.-- - .... --- -." "python"

โ—๏ธ๋น„๋‹ˆ's ํ’€์ด

  • ์ฒ˜์Œ์—” for...in ๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜๋ณต์‹œํ‚จ ๋’ค, key์™€ letter[i]๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•˜๋ ค ํ–ˆ์œผ๋‚˜, ๊ทธ๋ƒฅ ํ•ด๋‹น key๊ฐ’์— ๋งž๋Š” value๋งŒ ๊ฐ€์ ธ์˜ค๋ฉด ๋˜๋Š”๊ฑฐ๋ผ ์ฝ”๋“œ ์ˆ˜์ •์„ ํ–ˆ๋‹ค!
function solution(letter) {
    morse = { 
    '.-':'a',
    '-...':'b',
    '-.-.':'c',
    '-..':'d',
    '.':'e',
    '..-.':'f',
    '--.':'g',
    '....':'h',
    '..':'i',
    '.---':'j',
    '-.-':'k',
    '.-..':'l',
    '--':'m',
    '-.':'n',
    '---':'o',
    '.--.':'p',
    '--.-':'q',
    '.-.':'r',
    '...':'s',
    '-':'t',
    '..-':'u',
    '...-':'v',
    '.--':'w',
    '-..-':'x',
    '-.--':'y',
    '--..':'z'
	}
    let newLetter=letter.split(" ");
    let res="";
    	for(let i=0;i<newLetter.length;i++){
    	    res=res+morse[newLetter[i]];
   		}
    return res;
}

⊕ Extra Knowledge ++

  • string.split()
split() method๋Š” string ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•œ ๊ตฌ๋ถ„์ž๋ฅผ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ๋‚˜๋ˆˆ๋‹ค.
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]);
// Expected output: "fox"

const chars = str.split('');
console.log(chars[8]);
// Expected output: "k"

const strCopy = str.split();
console.log(strCopy);
// Expected output: Array ["The quick brown fox jumps over the lazy dog."]

์ด ๋•Œ ๋ฐ˜ํ™˜๊ฐ’์€ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ separator๋งˆ๋‹ค ๋Š์€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋‹ด์€ ARRAY!!


  • Object ํƒ์ƒ‰ (for in ๋ฌธ)
key ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ๋ฐฐ์—ด๋ณด๋‹ค ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ!
let obj = {
    a:1,
    b:2
}

for(let key in obj){
    console.log(key) //a b
    console.log(obj[key]) //1 2 
}
Comments