Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>PZ-Object</title>
- </head>
- <body>
- <script>
- 'use strict'
- /* Создать объект, хранящий в себе отдельно числитель и знаменатель дроби,
- и следующие функции для работы с этим объектом. */
- /* 1. Функция сложения 2-х объектов-дробей. */
- function printFract(fract, printer = (str) => console.log(str)) {
- printer(`${fract.num}/${fract.denom}`);
- }
- function addition(fractA, fractB) {
- return reduction({ num: (fractA.num * fractB.denom) + (fractB.num * fractA.denom), denom: fractA.denom * fractB.denom });
- }
- /* 2. Функция вычитания 2-х объектов-дробей. */
- function subtraction(fractA, fractB) {
- return reduction({ num: (fractA.num * fractB.denom) - (fractB.num * fractA.denom), denom: fractA.denom * fractB.denom });
- }
- /* 3. Функция умножения 2-х объектов-дробей. */
- function multiplication(fractA, fractB) {
- return reduction({ num: fractA.num * fractB.num, denom: fractA.denom * fractB.denom });
- }
- /* 4. Функция деления 2-х объектов-дробей. */
- function division(fractA, fractB) {
- return reduction({ num: fractA.num * fractB.denom, denom: fractA.denom * fractB.num });
- }
- /* 5. Функция сокращения объекта-дроби. */
- function reduction(fact) {
- let resOne;
- let resTwo;
- for (let i = 1; i < fact.num + fact.denom; ++i) {
- if (fact.num % i == 0 && fact.denom % i == 0) {
- resOne = fact.num / i;
- resTwo = fact.denom / i;
- } else if (fact.num == fact.denom) {
- resOne = 1;
- resTwo = 1;
- }
- }
- return { num: resOne, denom: resTwo }
- }
- {
- let fractOne = { num: 3, denom: 6 };
- let fractTwo = { num: 2, denom: 5 };
- printFract(addition(fractOne, fractTwo));
- printFract(subtraction(fractOne, fractTwo));
- printFract(multiplication(fractOne, fractTwo));
- printFract(division(fractOne, fractTwo));
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement