Advertisement
avr39ripe

jsPekarskiFractionObject

Feb 24th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <!doctype html>
  3. <html lang="en">
  4.  
  5.  
  6. <head>
  7.     <meta charset="utf-8">
  8.  
  9.     <title>PZ-Object</title>
  10. </head>
  11.  
  12. <body>
  13.  
  14.     <script>
  15.         'use strict'
  16.  
  17.         /* Создать объект, хранящий в себе отдельно числитель и знаменатель дроби,
  18.            и следующие функции для работы с этим объектом. */
  19.  
  20.         /* 1. Функция сложения 2-х объектов-дробей. */
  21.  
  22.         function printFract(fract, printer = (str) => console.log(str)) {
  23.             printer(`${fract.num}/${fract.denom}`);
  24.         }
  25.  
  26.         function addition(fractA, fractB) {
  27.             return reduction({ num: (fractA.num * fractB.denom) + (fractB.num * fractA.denom), denom: fractA.denom * fractB.denom });
  28.         }
  29.  
  30.         /* 2. Функция вычитания 2-х объектов-дробей. */
  31.  
  32.         function subtraction(fractA, fractB) {
  33.             return reduction({ num: (fractA.num * fractB.denom) - (fractB.num * fractA.denom), denom: fractA.denom * fractB.denom });
  34.         }
  35.  
  36.         /* 3. Функция умножения 2-х объектов-дробей. */
  37.  
  38.         function multiplication(fractA, fractB) {
  39.             return reduction({ num: fractA.num * fractB.num, denom: fractA.denom * fractB.denom });
  40.         }
  41.  
  42.         /* 4. Функция деления 2-х объектов-дробей. */
  43.  
  44.         function division(fractA, fractB) {
  45.             return reduction({ num: fractA.num * fractB.denom, denom: fractA.denom * fractB.num });
  46.         }
  47.  
  48.         /* 5. Функция сокращения объекта-дроби. */
  49.  
  50.         function reduction(fact) {
  51.             let resOne;
  52.             let resTwo;
  53.  
  54.             for (let i = 1; i < fact.num + fact.denom; ++i) {
  55.                 if (fact.num % i == 0 && fact.denom % i == 0) {
  56.                     resOne = fact.num / i;
  57.                     resTwo = fact.denom / i;
  58.                 } else if (fact.num == fact.denom) {
  59.                     resOne = 1;
  60.                     resTwo = 1;
  61.                 }
  62.             }
  63.             return { num: resOne, denom: resTwo }
  64.         }
  65.  
  66.         {
  67.             let fractOne = { num: 3, denom: 6 };
  68.             let fractTwo = { num: 2, denom: 5 };
  69.  
  70.             printFract(addition(fractOne, fractTwo));
  71.             printFract(subtraction(fractOne, fractTwo));
  72.             printFract(multiplication(fractOne, fractTwo));
  73.             printFract(division(fractOne, fractTwo));
  74.         }
  75.  
  76.  
  77.     </script>
  78. </body>
  79.  
  80. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement