๐Ÿ“œ ๋ชฉ์ฐจ

์•Œ๊ณ ๋ฆฌ์ฆ˜

์‹œ์ €์•”ํ˜ธ

NestJS

Encrypt / Hash

JWT / Passport

Guard

์•Œ๊ณ ๋ฆฌ์ฆ˜

๐Ÿ“Œ reduce()

<aside> โ— reduec()๋ฌธ ํ˜•์‹: ๋ฐฐ์—ด๋ช….reduce(์ฝœ๋ฐฑํ•จ์ˆ˜, ์ดˆ๊ธฐ๊ฐ’)

</aside>

<aside> โ— callback function ํ˜•์‹:

</aside>

const sum = [1,2,3,4,].reduce((acc,cur,idx,arr) => acc+cur)
console.log(sum) //10

Array.prototype.reduce() - JavaScript | MDN

ZeroCho Blog

๐Ÿ“Œ charCodeAt() / String.fromCharCode()

let name = "์ดํƒœ๋ฏผ"
name.charCodeAt(0) // 51060, '์ด'์— ํ•ด๋‹นํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’
name.charCodeAt(1) // 53468
name.charCodeAt(2) // 48124

String.fromCharCode(51060, 53468, 48124) // '์ดํƒœ๋ฏผ'

String.prototype.charCodeAt() - JavaScript | MDN

String.fromCharCode() - JavaScript | MDN


๐Ÿ’ญ ์‹œ์ € ์•”ํ˜ธ

// ์–ด๋–ค ๋ฌธ์žฅ์˜ ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ์ผ์ •ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ€์–ด์„œ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์•”ํ˜ธํ™” ๋ฐฉ์‹์„ ์‹œ์ € ์•”ํ˜ธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
// ์˜ˆ๋ฅผ ๋“ค์–ด "AB"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "BC"๊ฐ€ ๋˜๊ณ , 3๋งŒํผ ๋ฐ€๋ฉด "DE"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 
// "z"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "a"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 
// ๋ฌธ์ž์—ด s์™€ ๊ฑฐ๋ฆฌ n์„ ์ž…๋ ฅ๋ฐ›์•„ s๋ฅผ n๋งŒํผ ๋ฏผ ์•”ํ˜ธ๋ฌธ์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ๋ณด์„ธ์š”.

// ๊ณต๋ฐฑ์€ ์•„๋ฌด๋ฆฌ ๋ฐ€์–ด๋„ ๊ณต๋ฐฑ์ž…๋‹ˆ๋‹ค.
// s๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
// s์˜ ๊ธธ์ด๋Š” 8000์ดํ•˜์ž…๋‹ˆ๋‹ค.
// n์€ 1 ์ด์ƒ, 25์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

function solution(s,n){
	
}

// ๋ฌธ์ž์—ด 2๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ ์‹คํ–‰

const lower = 'abcdefghijklmnopqrstuvwxyz'
const upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
function solution(s,n){
    let answer = ""
    
    for(let i = 0; i <s.length; i++){
        if(s[i] === " "){
            answer +=s[i]
        } else {
            const word = lower.includes(s[i]) ? lower : upper
            let idx = word.indexOf(s[i]) + n
            
            if(word[idx] === undefined){
                idx -=26
            }
            answer +=word[idx]
        }
    }
    return answer
}

// reduce() ์‚ฌ์šฉ

const lower2 = 'abcdefghijklmnopqrstuvwxyz'
const upper2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

function solution(s,n){
    const answer = s.split("").reduce((acc,cur)=> {
        const word = lower.includes(cur) ? lower2 : upper2
        let idx = word.indexOf([cur]) + n
        
        if( idx >= 26){
            idx -=26
        } 
        return acc + (
            cur === " " ? " " : word[idx]
        )
        
    }, "")
    return answer
}

// ์•„์Šคํ‚ค ์ฝ”๋“œ ์ด์šฉํ•œ ํ’€์ด

// charCodeAt() : ๋ฌธ์ž์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฐ์ดํ„ฐ(๋ฒˆํ˜ธ)๋ฅผ ๋ฆฌํ„ด
// String.fromCharCode() : ์œ ๋‹ˆ์ฝ”๋“œ ๋ฐ์ดํ„ฐ(๋ฒˆํ˜ธ)๋ฅผ ๋ฌธ์ž๋กœ ๋ฆฌํ„ด 

// ๋Œ€๋ฌธ์ž : 65 ~ 90
// ์†Œ๋ฌธ์ž : 97 ~ 122
function solution(s,n){
    let answer = ""
    for (let i = 0 ; i < s.length; i++){
        if (s[i] === " ") {
            answer += " "
        }
        else {
            let idx = s[i].charCodeAt() + n
            // ์†Œ๋ฌธ์ž z๋ฅผ ๋„˜์–ด๊ฐ„๋‹ค๊ฑฐ๋‚˜
            // ๋Œ€๋ฌธ์ž Z๋ฅผ ๋„˜์–ด๊ฐ„๋‹ค๊ฑฐ๋‚˜ ๊ธฐ์กด์˜ idx ๊ฐ’์ด ์†Œ๋ฌธ์ž์ธ ๊ฒฝ์šฐ
            if (idx > 122 || ( idx > 90 && (idx - n) < 97 )){
                idx -=26
                }
            answer += String.fromCharCode(idx);
        }
    }
    return answer
}