Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function areaOfFigures(input) {
- figure = input[0];
- let sum = 0.0;
- if (figure === "circle") {
- let radius = Number(input[1]);
- sum = radius * radius * Math.PI; // Или: sum = radius ** 2 * Math.PI;
- } else if (figure === "triangle") {
- let lenght = Number(input[1]);
- let hight = Number(input[2]);
- sum = lenght * hight / 2;
- } else if (figure === "square") {
- let side = Number(input[1]);
- sum = side * side; // Или: sum = side ** 2
- } else {
- let sideA = Number(input[1]);
- let sideB = Number(input[2]);
- sum = sideA * sideB;
- }
- console.log(sum.toFixed(3));
- }
- РЕШЕНИЕ С РЕЧНИК:
- function areaOfFigures(input) {
- figure = input[0];
- a = Number(input[1]);
- b = Number(input[2]);
- formulas = {
- 'square': a ** 2,
- 'circle': a ** 2 * Math.PI,
- 'triangle': a * b / 2,
- 'rectangle': a * b
- };
- console.log(`${(formulas[figure]).toFixed(3)}`);
- }
- Тарикатско решение с тернарен оператор:
- function areaOfFigures(input) {
- if (input[0] == 'circle' || input[0] == 'square') {
- console.log((Number(input[1]) ** 2 * (input[0] === 'circle' ? Math.PI : 1)).toFixed(3));
- } else if (input[0] == 'triangle' || input[0] == 'rectangle') {
- console.log((Number(input[1]) * Number(input[2]) / (input[0] === 'triangle' ? 2 : 1)).toFixed(3));
- }
- }
Add Comment
Please, Sign In to add comment