avr39ripe

jsShiftNumbetLeftFor

Jan 31st, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.17 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6. </head>
  7. <body>
  8.     <script>
  9.         `use strict`
  10.  
  11.         // 7. Запросить у пользователя число и на сколько цифр его
  12.         // сдвинуть. Сдвинуть цифры числа и вывести результат (если
  13.         // число 123456 сдвинуть на 2 цифры, то получится 345612)
  14.  
  15.         let num = +prompt('Enter number to shift', 1000001);
  16.  
  17.         console.log(`num = ${num}`);
  18.  
  19.         let digitsCount = 0;
  20.         for (let numCnt = num; numCnt; ++digitsCount, numCnt = Math.floor(numCnt / 10));
  21.         console.log(`digitscount = ${digitsCount}`);
  22.  
  23.         let shift = +prompt(`Shift ${num} left for: `, 3);
  24.         shift %= (digitsCount);
  25.         let result = String(num);
  26.  
  27.         console.log(`shift = ${shift}`);
  28.  
  29.         // if there are SOME zeroes in original number this method do the job.
  30.         // We assume that user can't enter number with heading zeroes, so
  31.         // 10002 is correct number, but not 01002. This method IS BASED on such assumption.
  32.  
  33.         if (shift > 0) {
  34.             let div = 10 ** (digitsCount - shift);
  35.             console.log(`div = ${div}`);
  36.  
  37.             let head = String(Math.floor(num / div));
  38.  
  39.             div /= 10;
  40.             console.log(`div = ${div}`);
  41.  
  42.             let tail = '';
  43.  
  44.             while (div > 0.1) {
  45.                 tail += String((Math.floor(num / div)) % 10);
  46.                 div /= 10;
  47.                 console.log(`tail = ${tail}`);
  48.             }
  49.             result = tail + head;
  50.         }
  51.  
  52.         // If there are NO zeroes in original number this method do the job
  53.         //if (shift > 0) {
  54.         //    let div = 10 ** (digitsCount - shift);
  55.         //    console.log(`div = ${div}`);
  56.  
  57.         //    let head = String(Math.floor(num / div));
  58.         //    let tail = String(num % div);
  59.         //    console.log(`head = ${head}`);
  60.         //    console.log(`tail = ${tail}`);
  61.  
  62.         //    result = tail + head;
  63.         //}
  64.  
  65.         console.log(`result = ${result}`);
  66.     </script>
  67. </body>
  68. </html>
Add Comment
Please, Sign In to add comment