As I was building kimchichingu.com I decided that at the end of each section, a simple interactive quiz would help people see if they understood the lesson. Naturally, I set out to find a library for it because why would I make my own? After some googling I couldn’t find one that did what I wanted, most seemed to be more for polling or collecting info than a question answer style academic quiz, so I did what any good programmer would do and made my own!
As I set off to create the library I had to decide on a few things:
- “Do I use jQuery? If so which version do I want to use as a dependency?”
- “How do I want to create the quizes? Json based? Attribute based?”
- “How do I want to distribute it? Git? Npm? Something else?”
in typescript, that can be used via json or annotations. I’m not going to before
anyone with the gritty details in code as that can be seen in the repository
here. Instead I’ll talk a bit about my
experience building this in Typescript. I would say that the two hardest parts
such as real
protected access, as well as that when typescript checks
for type mismatches, it also checks the
HTMLElement types by default, whether
you want it to or not.
private into the mix, and also is just kind of clunky and more
of a hacky solution than real classes like in a more OOP language such as Java.
As a result, Typescript classes only check and enforce
and fields at compile time, and then all things are actually
public in the
searching for a reason that my
private fields were
public once compiled.
The second issue, the enforcing of
HTMLElement types by default, was a huge hassle
at the beginning when compiling things as
HTMLElement which is expected, and so you have to cast everything. I
this would not be as much of a hassle, but it definitely was for quizly. I could
have cast all of the
HTMLElement returns to any to avoid having to figure
and it’s family out, but as I would still have to cast just as many elements,
and I would lose all the benefits of type checking that Typescript gives.
I decided that learning which elements extend off of what would be useful as
a web developer in future and also to make a more robust project now.
That’s the simplified story of quizly and how it was built. Would I use typescript again though? I definitely would. The type checking, is invaluable, and the familiarity with a OOP type syntax made coding something of this nature much less painful. So, if you’re looking for a quiz library to use on your site that is light weight, or a not too hard to understand example of typescript, please consider using quizly!