응용 - 계산기 만들기
계산기 만들기
세팅
npm init -y
tsc --init --rootDir ./src --outDir ./dist --esModuleInterop --module commonjs --strict true --allowJS true --checkJS true
package.json의 scripts항목을 다음과 같이 변경
"scripts": {
"start": "tsc && node ./dist/index.js",
"build": "tsc --build",
"clean": "tsc --build --clean"
},
이제 현재 경로에서 src 디렉토리를 만들어 준다. vsc 에서 현재 디렉토리 위치를 기반으로 편집기를 열어서 코딩 준비를 하기 위해 아래와 같이 입력한다.
code .
코딩
scr 폴더 안에 index.ts 라는 파일을 만들고 아래와 같은 함수를 만들자
function assignGrade(average: number): string { ... }
조건 평균이 90점 이상이면 “A”, 80점 이상이면 “B”, 70점 이상이면 “C”, 60점 이상이면 “D”, 그 이하면 “F”를 주는 로직
Student interface를 만들어 준다.
interface Student {
name: string;
age: number;
scores: {
korean: number;
math: number;
society: number;
science: number;
english: number;
};
}
Student라는 타입의 객체를 받아서 평균을 계산하는 calculateAverage라는 함수를 만들자
function calculateAverage(student: Student): number { ... }
학생을 만드는 함수를 만들어보자
성적을 출력하는 함수를 만들어보자
이제 main 함수를 만들고 main 함수를 호출해보자
function main(): void {
const spartan = createStudent('Spartan', 30, 95, 89, 76, 90, 97);
printResult(spartan);
}
main(); // main 함수를 호출
실행
해당 명령어로 프로젝트를 빌드한다.
npm run build
그런 다음 아래를 입력하고 실행한다.
npm run start
Last updated