- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
All right triangles with specified perimeter in JavaScript
Problem
We are required to write a JavaScript function that takes in a number that specifies the perimeter for a triangle. Our function should return an array of all the triangle side triplets whose perimeter is same as specified by the input.
Example
Following is the code −
const perimeter = 120; const findAllRight = (perimeter = 1) => { const res = []; for(let a = 1; a <= perimeter; a++){ for(let b = a; b <= perimeter - a; b++){ for(let c = a; c <= a + b; c++){ if(a + b + c !== perimeter){ continue; }; if((a * 2) + (b * 2) === (c * 2)){ res.push([a, b, c]); }; }; }; }; return res; }; console.log(findAllRight(perimeter));
Output
Following is the console output −
[ [ 1, 59, 60 ], [ 2, 58, 60 ], [ 3, 57, 60 ], [ 4, 56, 60 ], [ 5, 55, 60 ], [ 6, 54, 60 ], [ 7, 53, 60 ], [ 8, 52, 60 ], [ 9, 51, 60 ], [ 10, 50, 60 ], [ 11, 49, 60 ], [ 12, 48, 60 ], [ 13, 47, 60 ], [ 14, 46, 60 ], [ 15, 45, 60 ], [ 16, 44, 60 ], [ 17, 43, 60 ], [ 18, 42, 60 ], [ 19, 41, 60 ], [ 20, 40, 60 ], [ 21, 39, 60 ], [ 22, 38, 60 ], [ 23, 37, 60 ], [ 24, 36, 60 ], [ 25, 35, 60 ], [ 26, 34, 60 ], [ 27, 33, 60 ], [ 28, 32, 60 ], [ 29, 31, 60 ], [ 30, 30, 60 ] ]
- Related Articles
- Isosceles triangles with nearest perimeter using JavaScript
- Count number of right triangles possible with a given perimeter in C++
- How to set all the border right properties in one declaration with JavaScript?
- Sum of perimeter of all the squares in a rectangle using JavaScript
- Picking the triangle edges with maximum perimeter JavaScript
- Insert a specified element in a specified position in JavaScript?
- Segregate all 0s on right and 1s on left in JavaScript
- Replace all occurrences of specified element of ArrayList with Java Collections
- Finding the element larger than all elements on right - JavaScript
- Print all the paths from root, with a specified sum in Binary tree in C++
- Return a splitted array of the string based on all the specified separators - JavaScript
- Find the area and perimeter of right triangle in PL/SQL
- Select elements with the specified attribute starting with the specified value with CSS
- Insert a specified HTML text into a specified position in the JavaScript document?
- Sides of triangles are given below. Determine which of them are right triangles. In case of a right triangle, write the length of its hypotenuse.3 cm, 8 cm, 6 cm

Advertisements