카테고리 없음

(5장) Secret Secrets - 3.The Golden Code

띵킹 2023. 4. 14. 00:38

https://github.com/LearningTypeScript/projects/tree/main//projects/functions/secret-secrets/03-the-golden-code

 

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

아주 좋아요.
당신의 고도의 암호는 악당 골드버거와 
온 박사와 그들의 지원을 받는 사악한 조직인 
S.C.R.I.P.T.E.R.의 공격에 저항했습니다.

우리는 이 범죄자들에 대해 
충분히 오랫동안 방어를 해왔습니다.
공격할 시간입니다.
그들은 _Golden Code_라는 코드명을 가진 
그들만의 암호 기술을 가지고 있습니다

당신의 마지막 과제는 
createCodeCracker 함수를 개발하는 것입니다.
아래 설명된 대로 설정 객체를 사용해야 합니다.
코드가 주어지면 반복적으로 추측을 하고 추측의 타당성을 확인하는 함수를 반환합니다.
우리는 S.C.R.I.P.T.E.R.에 의해 사용된 비밀 메시지에 침입하여 그들을 완전히 물리칠 것입니다!

## 명세

매개 변수:

1. 다음과 같은 프로퍼티를 가진 객체:

- "attempts": 코드 크래킹을 얼마나 실행할 것인가

- 'makeGuess': 다음과 같은 함수
- 매개변수:
1. 'text': 임의의 문자열
2. "attempt": 시도가 몇번째 발생했는가
- 반환: 추측으로 사용할 문자열

- "validateGuess": 다음과 같은 함수
- 매개 변수:
1. "guess": 임의의 문자열
- 반환: 추측이 정확한지 여부

createCodeCracker가 반환: 다음과 같은 함수

- 매개변수:
1. 'text': 임의의 문자열
- 반환: makeGuess의 결과로 validateGuess를 실행시켜서, 참이 반환되면 그 결과를 반환하고, 그렇지 못하게 되면 undefined를 반환한다.

## 파일

- "index.ts": "createCodeCracker" 함수를 여기에 적습니다
- "index.test.ts": createCodeCracker'를 확인하는 테스트
- "solution.ts": 솔루션 코드
// Write your createCodeCracker function here! ✨
// You'll need to export it so the tests can run it.
type T = {
	attempts: number;
	makeGuess: (text: string, attempt: number) => string;
	validateGuess: (guess: string) => boolean;
};
export const createCodeCracker = (T: T) => {
	return (text: string) => {
		for (let i = 0; i < T.attempts; i++) {
			if (T.validateGuess(T.makeGuess(text, i))) {
				return T.makeGuess(text, i);
			}
		}
		return undefined;
	};
};

객체에 함수 시그니처를 사용해보는 문제

728x90