// kbunitscvt.js - Sample KB units converter // Original authors: Tactician and OldCoder // License: MIT/X // Revision: 130524 //-------------------------------------------------------------------- // important note //-------------------------------------------------------------------- // This software is provided on an AS IS basis with ABSOLUTELY NO WAR- // RANTY. The entire risk as to the quality and performance of the // software is with you. Should the software prove defective, you as- // sume the cost of all necessary servicing, repair or correction. In // no event will any of the developers, or any other party, be liable // to anyone for damages arising out of use of the software, or inab- // ility to use the software. //-------------------------------------------------------------------- // overview //-------------------------------------------------------------------- // This is a simple JavaScript program intended for people who are // learning the language. // This program prompts the user for a file size, expressed in kilo- // bytes, and displays the same size in the most appropriate units: // kilobytes, megabytes, gigabytes, or terabytes. // To try this code, load it in a trivial HTML file as follows and // visit the HTML file in a browser: // // // // // // //-------------------------------------------------------------------- // constants //-------------------------------------------------------------------- // KB_per_MB = Kilobytes per megabyte // MB_per_GB = Megabytes per gigabyte // GB_per_TB = Gigabytes per terabyte // KB_per_GB = Kilobytes per gigabyte // KB_per_TB = Kilobytes per terabyte var KB_per_MB = 1024; var MB_per_GB = 1024; var GB_per_TB = 1024; var KB_per_GB = KB_per_MB * MB_per_GB; var KB_per_TB = KB_per_GB * GB_per_TB; //-------------------------------------------------------------------- // get input from user //-------------------------------------------------------------------- // "Prompt" string var prompt_size = "Type file size in KB and press Enter"; // Prompt the user var file_size = prompt (prompt_size); while (isNaN (file_size)) // Repeat as necessary { alert ("Sorry, that is not a number"); var file_size = prompt (prompt_size); } //-------------------------------------------------------------------- // convert to appropriate units //-------------------------------------------------------------------- var units_str = "KB"; // Units string (KB, MB, GB, or TB) if ((file_size >= KB_per_MB) && (file_size < KB_per_GB)) { // Express as megabytes file_size = file_size / KB_per_MB; units_str = "MB"; } else if ((file_size >= KB_per_GB) && (file_size < KB_per_TB)) { // Express as gigabytes file_size = file_size / KB_per_GB; units_str = "GB"; } else if (file_size >= KB_per_TB) { // Express as terabytes file_size = file_size / KB_per_TB; units_str = "TB"; } // Round the result to nearest integer file_size = Math.round ((file_size * 100) / 100); // Output the result as HTML document.write ("

The file is ", file_size, " ", units_str, " in size

");