Advertisement
GatinhoMeow

Enter a number, get Infos

Mar 7th, 2024 (edited)
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 9.08 KB | Source Code | 0 0
  1. https://jsfiddle.net/SpectatorPrivate/4g3trn89/show
  2.  
  3. HTML:
  4.  
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <title>Number Info</title>
  11. <link rel="stylesheet" type="text/css" href="styles.css">
  12. </head>
  13. <body>
  14. <div>
  15.   <h1>Number Info</h1>
  16.   <label for="numberInput">Enter a number:</label>
  17.   <input type="text" id="numberInput">
  18.   <button id="getInfoButton">Get Info</button>
  19.   <div id="result"></div>
  20. </div>
  21.  
  22. <script src="script.js"></script>
  23. </body>
  24. </html>
  25.  
  26. JavaScript:
  27.  
  28. document.addEventListener('DOMContentLoaded', function() {
  29.   const getInfoButton = document.getElementById('getInfoButton');
  30.   const resultDiv = document.getElementById('result');
  31.  
  32.   getInfoButton.addEventListener('click', function() {
  33.     const number = parseInt(document.getElementById('numberInput').value);
  34.     const data = calculateInfo(number);
  35.     displayInfo(data, number);
  36.   });
  37.  
  38.   function calculateInfo(number) {
  39.     const englishRepresentation = convertToEnglish(number);
  40.     const todayDate = new Date().toString();
  41.     const millisecondsToSeconds = (number / 1000).toFixed(2) + " seconds";
  42.     const secondsToMinutes = (number / 60).toFixed(2) + " minutes";
  43.     const minutesToHour = (number / 60).toFixed(2) + " hour";
  44.     const hourToDays = (number / 24).toFixed(2) + " days";
  45.     const daysToYear = (number / 365.25).toFixed(5) + " year"; // considerando um ano bissexto
  46.     const daysToLeapYear = (number / 366).toFixed(5) + " year"; // considerando um ano bissexto
  47.     const yearToCentury = (number / 100).toFixed(2) + " century";
  48.  
  49.     const involution = Math.pow(number, 2);
  50.     const bytesToKilobytes = (number / 1024).toFixed(7) + " Kilobytes";
  51.     const bytesToMegabytes = (number / (1024 * 1024)).toFixed(15) + " Megabytes";
  52.     const bytesToGigabytes = (number / (1024 * 1024 * 1024)).toFixed(18) + " Gigabytes";
  53.     const bytesToTerabytes = (number / (1024 * 1024 * 1024 * 1024)).toFixed(21) + " Terabytes";
  54.     const bytesToPetabytes = (number / (1024 * 1024 * 1024 * 1024 * 1024)).toFixed(24) + " Petabytes";
  55.     const bytesToBits = number * 8 + " Bits";
  56.     const bytesToKilobits = (number * 8 / 1024).toFixed(5) + " Kilobits";
  57.     const bytesToMegabits = (number * 8 / (1024 * 1024)).toFixed(11) + " Megabits";
  58.     const bytesToGigabits = (number * 8 / (1024 * 1024 * 1024)).toFixed(14) + " Gigabits";
  59.     const decimalToBinary = number.toString(2);
  60.     const decimalToOctal = number.toString(8);
  61.     const decimalToHexadecimal = number.toString(16);
  62.     const asciiToBinary = number.toString().split('').map(char => char.charCodeAt(0).toString(2)).join(' ');
  63.     const asciiToText = String.fromCharCode(number);
  64.     const metersToCentimeter = (number * 100).toFixed(2) + " centimeter";
  65.     const metersToMillimeter = (number * 1000).toFixed(2) + " millimeter";
  66.     const metersToKilometer = (number / 1000).toFixed(2) + " kilometer";
  67.     const metersToFeets = (number * 3.28084).toFixed(2) + " feets";
  68.     const metersToMiles = (number * 0.000621371).toFixed(11) + " miles";
  69.     const metersToInch = (number * 39.3701).toFixed(2) + " inch";
  70.     const metersToLeague = (number * 0.000207123).toFixed(13) + " league";
  71.     const metersToNauticalLeague = (number * 0.000179986).toFixed(12) + " league [nautical]";
  72.     const metersToMicroinch = (number * 39370078.7402).toFixed(2) + " microinch";
  73.     const metersToYard = (number * 1.09361).toFixed(2) + " yard";
  74.  
  75.     return {
  76.       englishRepresentation,
  77.       todayDate,
  78.       millisecondsToSeconds,
  79.       secondsToMinutes,
  80.       minutesToHour,
  81.       hourToDays,
  82.       daysToYear,
  83.       daysToLeapYear,
  84.       yearToCentury,
  85.       involution,
  86.       bytesToKilobytes,
  87.       bytesToMegabytes,
  88.       bytesToGigabytes,
  89.       bytesToTerabytes,
  90.       bytesToPetabytes,
  91.       bytesToBits,
  92.       bytesToKilobits,
  93.       bytesToMegabits,
  94.       bytesToGigabits,
  95.       decimalToBinary,
  96.       decimalToOctal,
  97.       decimalToHexadecimal,
  98.       asciiToBinary,
  99.       asciiToText,
  100.       metersToCentimeter,
  101.       metersToMillimeter,
  102.       metersToKilometer,
  103.       metersToFeets,
  104.       metersToMiles,
  105.       metersToInch,
  106.       metersToLeague,
  107.       metersToNauticalLeague,
  108.       metersToMicroinch,
  109.       metersToYard,
  110.       number
  111.     };
  112.   }
  113.  
  114.   function convertToEnglish(number) {
  115.     const singleDigits = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
  116.     const teenDigits = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
  117.     const tensDigits = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
  118.     const scales = ['', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion', 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion', 'Decillion'];
  119.  
  120.     if (number === 0) return 'Zero';
  121.     if (number < 0) return 'Negative ' + convertToEnglish(-number);
  122.  
  123.     let groupCount = 0;
  124.     let result = '';
  125.  
  126.     while (number > 0) {
  127.       if (number % 1000 !== 0) {
  128.         let tempResult = convertToEnglishLessThanThousand(number % 1000);
  129.  
  130.         if (groupCount > 0) {
  131.           tempResult += ' ' + scales[groupCount];
  132.         }
  133.  
  134.         result = tempResult + ' ' + result;
  135.       }
  136.  
  137.       number = Math.floor(number / 1000);
  138.       groupCount++;
  139.     }
  140.  
  141.     return result.trim();
  142.   }
  143.  
  144.   function convertToEnglishLessThanThousand(number) {
  145.     const singleDigits = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
  146.     const teenDigits = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
  147.     const tensDigits = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
  148.  
  149.     let result = '';
  150.  
  151.     if (number >= 100) {
  152.       result += singleDigits[Math.floor(number / 100)] + ' Hundred ';
  153.       number %= 100;
  154.     }
  155.  
  156.     if (number >= 20) {
  157.       result += tensDigits[Math.floor(number / 10)] + ' ';
  158.       number %= 10;
  159.     } else if (number >= 10) {
  160.       result += teenDigits[number - 10] + ' ';
  161.       number = 0;
  162.     }
  163.  
  164.     if (number > 0) {
  165.       result += singleDigits[number] + ' ';
  166.     }
  167.  
  168.     return result.trim();
  169.   }
  170.  
  171.   function displayInfo(data, number) {
  172.     resultDiv.innerHTML = `
  173.       <div style="text-align: center;">
  174.         <p style="font-size: 64px;"><strong>${number}</strong></p>
  175.         <p style="font-size: 36px; font-weight: bold;">${data.englishRepresentation}</p>
  176.       </div>
  177.       <div>
  178.         <p>Time data:</p>
  179.         <ul>
  180.           <li>Today's date: ${data.todayDate}</li>
  181.          <li>Milliseconds to seconds: ${data.millisecondsToSeconds}</li>
  182.          <li>Seconds to minutes: ${data.secondsToMinutes}</li>
  183.          <li>Minutes to hour: ${data.minutesToHour}</li>
  184.          <li>Hour to days: ${data.hourToDays}</li>
  185.          <li>Days to year: ${data.daysToYear}</li>
  186.          <li>Days to a leap year: ${data.daysToLeapYear}</li>
  187.          <li>Year to century: ${data.yearToCentury}</li>
  188.        </ul>
  189.        <p>IT data:</p>
  190.        <ul>
  191.          <li>Involution of ${number} is ${data.involution}</li>
  192.          <li>${number} bytes is ${data.bytesToKilobytes}</li>
  193.          <li>${number} bytes is ${data.bytesToMegabytes}</li>
  194.          <li>${number} bytes is ${data.bytesToGigabytes}</li>
  195.          <li>${number} bytes is ${data.bytesToTerabytes}</li>
  196.          <li>${number} bytes is ${data.bytesToPetabytes}</li>
  197.          <li>${number} bytes is ${data.bytesToBits}</li>
  198.          <li>${number} bytes is ${data.bytesToKilobits}</li>
  199.          <li>${number} bytes is ${data.bytesToMegabits}</li>
  200.          <li>${number} bytes is ${data.bytesToGigabits}</li>
  201.          <li>Decimal to binary is ${data.decimalToBinary}</li>
  202.          <li>Decimal to octal is ${data.decimalToOctal}</li>
  203.          <li>Decimal to hexadecimal is ${data.decimalToHexadecimal}</li>
  204.          <li>${number} ASCII to Binary is ${data.asciiToBinary}</li>
  205.          <li>${number} ASCII to Text is ${data.asciiToText}</li>
  206.        </ul>
  207.        <p>Distance data:</p>
  208.        <ul>
  209.          <li>${number} meters is ${data.metersToCentimeter}</li>
  210.          <li>${number} meters is ${data.metersToMillimeter}</li>
  211.          <li>${number} meters is ${data.metersToKilometer}</li>
  212.          <li>${number} meters is ${data.metersToFeets}</li>
  213.          <li>${number} meters is ${data.metersToMiles}</li>
  214.          <li>${number} meters is ${data.metersToInch}</li>
  215.          <li>${number} meters is ${data.metersToLeague}</li>
  216.          <li>${number} meters is ${data.metersToNauticalLeague}</li>
  217.          <li>${number} meters is ${data.metersToMicroinch}</li>
  218.          <li>${number} meters is ${data.metersToYard}</li>
  219.        </ul>
  220.      </div>
  221.    `;
  222.  }
  223. });
  224.  
  225.  
  226. CSS:
  227.  
  228. .container {
  229.  max-width: 600px;
  230.  margin: 0 auto;
  231.  text-align: center;
  232. }
  233.  
  234. input[type="text"] {
  235.  width: 100px;
  236.  margin: 10px;
  237. }
  238.  
  239. button {
  240.  padding: 10px 20px;
  241.  background-color: #007bff;
  242.  color: #fff;
  243.  border: none;
  244.  cursor: pointer;
  245. }
  246.  
  247. button:hover {
  248.  background-color: #0056b3;
  249. }
  250.  
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement