How To Create Byte Calculator Using JavaScript

Byte Calculator Using JavaScript

In this article, we are going to learn on how to create Byte Calculator Using JavaScript. This program creates using JavaScript. In the previous tutorial, we created a lot of tutorials all about the calculator, but this time, this kind of calculator that we are going to create is different. This calculator is for the Byte, KiloByte, MegaByte, and GigaByte.

Create Simple Form Input Field

  1. <form name="bandwidth" method="post" style="margin:auto; width:500px;">
  2.         <input type="text" name="original" size="20" style="color:blue; background:azure; text-indent:5px; border:1px solid blue; font-size:18px; font-family:cursive; font-weight:bold;" autofocus="autofocus" />
  3.         <select size="1" name="units" style="color:red; width:100px; background:azure; text-indent:5px; border:1px solid blue; font-size:18px; font-family:cursive; font-weight:bold;">
  4.                 <option value="Bytes">Bytes</option>
  5.                 <option value="Kb">Kb</option>
  6.                 <option value="Mb">Mb</option>
  7.                 <option value="Gb">Gb</option>
  8.         </select>
  9.         <br />
  10.         <br />
  11.         <input type="button" value="Calculate" name="B1" onClick="get_the_Result()" style="color:red; background:azure; border-radius:8px; text-indent:5px; border:1px solid blue; font-size:18px; font-family:cursive; font-weight:bold;">
  12. </form>

JavaScript Script

  1. <script>
  2. var value_Of_Bytes=0
  3. function get_the_Result(){
  4. var the_Value=document.bandwidth.original.value
  5. var selectunit=document.bandwidth.units.options[document.bandwidth.units.selectedIndex].value
  6. if (selectunit=="Bytes")
  7. value_Of_Bytes=the_Value
  8. else if (selectunit=="Kb")
  9. value_Of_Bytes=the_Value*1024
  10. else if (selectunit=="Mb")
  11. value_Of_Bytes=the_Value*1024*1024
  12. else if (selectunit=="Gb")
  13. value_Of_Bytes=the_Value*1024*1024*1024
  14.  
  15. alert (the_Value+" "+selectunit+" is equal to:\n\n- "+value_Of_Bytes+" Bytes\n- "+Math.round(value_Of_Bytes/1024)+" Kb\n- "+Math.round(value_Of_Bytes/1024/1024)+" Mb\n- "+Math.round(value_Of_Bytes/1024/1024/1024)+" Gb\n")
  16. }
  17. </script>

Result:

Enter the value and select type of converter. Result This is the result. Result So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment