Unit Converter

Unit Converter interface

The unit converter project is part of the practice time of the "Learn JavaScript" module of Scrimba

This is a part of my basic JavaScript practice specifically on the use of event listeners, template literals, and toFixed method.

The "Convert" button has an event listener in which when clicked, it triggers a function to convert the value entered in the number input. However, the value being entered inside the input is a string so we used .parseFloat in the script, to convert the string into a number and enable the numerical operations to proceed.

The input value is assigned to a constant variable named `baseNum` - this variable will be used inside the template literal string. Also, the template literal will render the initial input value, and the computed value to the desired unit of conversion.

The input value will be multiplied to the corresponding value of the respective unit of conversion. The target unit are as follows:

As we have mentioned earlier, there is a template literal that define strings and allow expressions (e.g. multiplying the initial value to the desired unit of conversion), and the answer to the conversion of units will be rendered through .textContent property.

The snippet example is retrieved from the length conversion. (E.g. we convert number 10 to the length units of Meter and Feet)

       
        // length conversion (meter to feet and vice versa)
        lengthEl.textContent = 
        `${baseNum} meters = ${(baseNum * meter2feet).toFixed(3)} feet | 
        ${baseNum} feet = ${(baseNum * feet2meter).toFixed(3)} meters`;
      
    

As being observed also in the template literal is the use of .toFixed method. This helped me to round off the returned value to three (3) decimal places.

With this snippet, the rendered text content will be:
10 meters = 32.810 feet | 10 feet = 3.048 meters