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
문제 간단 요약 :
## 문제
개선해야 할 첫 번째 프로젝트는 비교적 오래된 JavaScript 스타일로 작성되었습니다.
original.js 파일에 쓰여진 것과 같이 이것의 AnnounceMachines 함수는
announce 함수와 임의 개수의 machine 객체를 받기 위한 것입니다. 다음 두 가지 작업을 수행합니다.
각 machine 객체에 대해 announce를 호출합니다.
machine의 label 프로퍼티가 존재하는 경우 사용하고,
그렇지 않은 경우 make와 model 프로퍼티를 통해 label을 생성합니다.
label 프로퍼티가 있는machine 객체 수를 나타내는 숫자를 반환합니다.
이 함수는 변수에 대해 var를 사용하고,
동적 매개변수를 활용하기 위해 arguments를 사용합니다.
또한 문자열을 연결하기 위해 + 연산자를 사용합니다.
귀하의 임무는 보다 현대적인 JavaScript 구문을 사용하여 함수를 다시 작성하는 것입니다.
## 명세
'index.js'에서 다음과 같은 'anouncementMachines' 함수를 내보냅니다:
- 변수에는 var 대신 let와 const를 사용합니다
- arguments 대신 ... 스프레드를 사용합니다
- for문 대신 'for'-of' 루프를 사용합니다
- '+' 대신 템플릿 리터럴을 사용해서 문자를 연결합니다
## 파일
- "index.js": 여기에 `announceMachines` 기능을 적으십시오
- "index.test.js": 'anouncementMachines'를 확인하는 테스트 파일
- "solution.js": 솔루션 코드
function announceMachines(announce) {
var label;
var labelsCount = 0;
for (var i = 1; i < arguments.length; i += 1) {
const machine = arguments[i];
if (machine.label) {
label = machine.label;
labelsCount += 1;
} else {
label = "Make: " + machine.make + "; Model: " + machine.model;
}
announce(label);
}
return labelsCount;
}
원본 코드는 함수의 arguments 속성을 활용한다.
announce 함수를 제외한 나머지 매개변수 machine 객체에서 label을 추출하고, 없는 경우 생성해서 announce 함수를 호출한다. label이 있는 갯수 만큼 Count를 반환한다.
function announceMachines(announce, ...machine) {
let labelsCount = 0;
for (const m of machine) {
let label;
if (m.label) {
label = m.label;
labelsCount += 1;
} else {
label = `Make: ${m.make}; Model: ${m.model}`;
}
announce(label);
}
return labelsCount;
}
문제에서 요구한대로 재정의한 함수다. es6의 나머지 매개변수 기능을 활용해서 machine을 배열로 받고, for of 문으로 순회한다.
잘 돌아간다.
728x90
'Typescript > 러닝 타입스크립트 연습문제' 카테고리의 다른 글
(3장) Primitive Cooking - 1. Ingredients (0) | 2023.04.06 |
---|---|
(2장) System of a Clown - 2. Clown Availability (0) | 2023.04.06 |
(2장) System of a Clown - 1. Clowning Around (0) | 2023.04.06 |
(1장) The Typeinator - 3. Callbacks to Async Await (0) | 2023.04.05 |
(1장) The Typeinator - 2. Prototypes to Classes (0) | 2023.04.05 |