https://github.com/LearningTypeScript/projects/tree/main//projects/functions/secret-secrets/02-dr-on
GitHub - LearningTypeScript/projects: Hands-on real world projects that will help you exercise your knowledge of TypeScript.
Hands-on real world projects that will help you exercise your knowledge of TypeScript. - GitHub - LearningTypeScript/projects: Hands-on real world projects that will help you exercise your knowledg...
github.com
음.
아주 좋아요.
당신의 암호는 우리의 문자를 해독하는데 충분했습니다.
우리의 경쟁자인 사악한 골드버거가 또다시 좌절되었습니다.
하지만!
골드버거는 유명한 범죄 암호학자인 온 박사의 도움을 받아 우리의 암호를 해독한 것 같습니다.
암호 해독 작업을 방해하기 위해 고급 암호 작성기를 개발하는 데 귀하의 도움이 필요합니다.
당신의 일은 "createAdvancedCipher" 함수를 개발하는 것입니다.
기존의 createCipher 함수와 비슷하지만 함수 파라미터를 하나만 입력하는 대신 3개를 입력합니다.
모음 하나, 자음 하나, 구두점 하나.
## 명세
매개 변수:
1. onVowel : 문자열을 입력하고 다른 문자열을 반환하는 함수
2. onConsonant : 문자열을 입력하고 다른 문자열을 반환하는 함수
3. onPunctuation : 문자열을 입력하고 다른 문자열을 반환하는 함수
반환: 다음과 같은 함수
- 매개 변수:
1. 'text': 임의의 문자열
- 반환: 'text'의 모든 문자에 대해 정해진 함수를 호출하여 생성된 문자열:
만약 문자가('/[aeiou]/i')인 경우: onVowel
문자가 자음('/[bcdfghjklmnpqrstvxyz]/i')인 경우: onConsonant
그 외에 : onPunctuation
## 파일
- "index.ts": "createAdvancedCipher" 함수를 여기에 적습니다
- "index.test.ts": 'create Advanced Cipher'를 확인하는 테스트
- "solution.ts": 솔루션 코드
type onVowel = (token:string) => string
type onConsonant = (token:string) => string
type onPunctuation = (token:string) => string
export const createAdvancedCipher = (onVowel:onVowel,onConsonant:onConsonant,onPunctuation:onPunctuation) => {
return (text:string) => {
let newText = ''
const vowelReg = /[aeiou]/i;
const consonantReg = /[bcdfghjklmnpqrstvwxyz]/i;
for (const token of text){
if(vowelReg.test(token)){
newText += onVowel(token)
} else if(consonantReg.test(token)) {
newText += onConsonant(token)
} else {
newText += onPunctuation(token)
}
}
return newText
}
}
이전 문제와 비슷하나, 문자를 구분해서 각기 다른 암호화 함수를 적용해야 한다.
솔루션에서는 삼항연산자를 통해 cipher 함수를 조건에 따라 할당해서 호출하는 깔끔한 코드였다.
728x90
'Typescript > 러닝 타입스크립트 연습문제' 카테고리의 다른 글
(5장) Secret Secrets - 1.Incoming Cipher (0) | 2023.04.13 |
---|---|
(4장) Various Lawyerings - 3.Making Arguments (0) | 2023.04.12 |
(4장) Various Lawyerings - 2.Case Management (0) | 2023.04.12 |
(4장) Various Lawyerings - 1.Aviary Classification (0) | 2023.04.12 |
(3장 심화) The Narrow Trail (0) | 2023.04.06 |