Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function OperationsBetweenNumbers(input) {
- let num1 = Number(input[0]);
- let num2 = Number(input[1]);
- let symbol = input[2];
- let number = "";
- let sum = 0;
- if (symbol === "+" || symbol === "-" || symbol === "*") {
- if (symbol === "+") {
- sum = num1 + num2;
- } else if (symbol === "-") {
- sum = num1 - num2;
- } else {
- sum = num1 * num2;
- }
- if (sum % 2 === 0) {
- number = "even";
- } else {
- number = "odd";
- }
- console.log(`${num1} ${symbol} ${num2} = ${sum} - ${number}`);
- } else {
- if (num2 === 0) {
- console.log(`Cannot divide ${num1} by zero`);
- } else {
- if (symbol === "/") {
- sum = num1 / num2;
- console.log(`${num1} / ${num2} = ${sum.toFixed(2)}`);
- } else if (symbol === "%") {
- sum = num1 % num2;
- console.log(`${num1} % ${num2} = ${sum}`);
- }
- }
- }
- }
- Решение с тернарен оператор:
- function OperationsBetweenNumbers(input) {
- let num1 = Number(input[0]);
- let num2 = Number(input[1]);
- let symbol = input[2];
- let sum =
- symbol === "+" ? num1 + num2 :
- symbol === "-" ? num1 - num2 :
- symbol === "*" ? num1 * num2 :
- symbol === "/" && num2 !== 0 ? num1 / num2 :
- symbol === "%" && num2 !== 0 ? num1 % num2 : 0;
- let number = sum % 2 === 0 ? "even" : "odd";
- if (symbol === "+" || symbol === "-" || symbol === "*") {
- console.log(`${num1} ${symbol} ${num2} = ${sum} - ${number}`);
- } else if (symbol === "/" || symbol === "%") {
- console.log(num2 === 0 ? `Cannot divide ${num1} by zero` :
- `${num1} ${symbol} ${num2} = ${symbol === "/" ? sum.toFixed(2) : sum}`);
- }
- }
Add Comment
Please, Sign In to add comment