Hits and Points

Time for a small project! In this checkpoint, you'll write a whole program on your own, using what you've learned in the first three lessons: variables and arrays with their types, conditions, and functions with typed parameters and return values.

The exercises here are a bit different from the ones so far. The instructions say what the program should print, not how to write it. There are many correct ways, so pick the one that makes sense to you. Just remember that TypeScript runs a program only when it finds no type error in it, so your program has to be free of them. If you get stuck, look back at the lesson where you learned what you need.

The project. The village holds an archery contest at its summer fair, and you're writing the scoreboard for the judges. Over three exercises, your program will count an archer's hits and points, award a medal, and finally compare two archers and announce the winner. Each exercise continues where the previous one ended: the editor starts with a finished program from the previous step, which may look different from yours. That's fine.

The data. The constants at the top of the program hold what the judges wrote down: archer is the archer's name, and arrows, an array of the type number[], holds the points of each arrow, from 0 to 10. An arrow with 0 points missed the target. In a real app, the judges would type the points in. Here, the program gets them from the constants, and your code should work with the constants, not with their values copied into it: when you change them, the program should still print the right result. To check that, each exercise also runs your program with other data, and it has to get those right too.

The task. Kira shot 6 arrows, and 5 of them hit the target. Together, they're worth 40 points. The program prints:

Archer: Kira
Hits: 5 of 6 arrows
Points: 40

A tip: give each calculation its own function, for example one that takes the arrows and returns their points. Remember from the Functions lesson that every parameter needs a type, and that with a return type, TypeScript checks what the function returns. The functions will come in handy in the last exercise.

Instructions

Print the archer's name and how many of the arrows hit the target, as in the example.

Below that, print the points of all the arrows together.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/3