Live
Repo
Unit Converter
This solo project taught me a lot, thanks to the amazing Scrimba course and teachers!
The requirements were:
... but I decided to make the design of it a little bit better so I can enjoy creating something custom and bring to it a bit of myself.
- Instead of making a button, I decided to create an input and display the conversion straight away on an input change. Along with this, I learned a new event listener ("input"). The input event fires when the value of an ,
- To provide a better UI, I made the input autofocused, using the attribute of the same name.
- In order to meet the third requirement I learned the toFixed() method, which converts a number to a specified number of decimals.
- I found out how to make a gradient background, using the background-image CSS property.
- I faced one problem, which led me to new knowledge. Firstly, I repeated the same code of unit calculation and HTML rendering in the global scope and on input change. I should reassign the value of units because the input value changes and it is one of the multipliers. Instead, I created 2 functions: one is for the maths calculation and the other one is for rendering units in an HTML document. Then I invoked them twice - in the global scope and in the function when the input value changes. Thus I made my code DRYer.
- I added an option to switch the mode to the "light" one. Firstly I created the toggle switch button, adding some icons with font awesome. Then, using the "change" event listener on an input of checkbox type, I toggled the "dark" class on elements I needed to style differently. In CSS I added the "dark" class to the corresponding elements.