Isosceles triangles with nearest perimeter using JavaScript

JavascriptWeb DevelopmentFront End Technology

<h2>Almost Isosceles Triangle</h2><p>An Almost Isosceles Integer Triangle is a triangle that all its side lengths are integers and also, two sides are almost equal, being their absolute difference 1 unit of length.</p><h2>Problem</h2><p>We are required to write a JavaScript function that takes in a number which specifies the perimeter of a triangle.</p><p>Our function should find the measurement of such an almost isosceles triangle whose perimeter is nearest to the input perimeter.</p><p>For example, if the desired perimeter is 500,</p><p>Then the almost isosceles triangle with the nearest perimeter will be &minus; [105, 104, 181]</p><h2>Example</h2><p>Following is the code &minus;</p><p><a class="demo" href="" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notransalte">const perimeter = 500; &nbsp;const almostIsosceles = (perimeter = 0) =&gt; { &nbsp;let a = perimeter; &nbsp;for(; a &gt; 0; a--){ &nbsp; &nbsp; &nbsp; &nbsp; for(let b = perimeter; b &gt; 0; b--){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let c = perimeter; c &gt; 0; c--){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a + b + c &gt; perimeter || a !== b + 1 || (Math.pow(a, 3) - Math.pow(b, 3) !== Math.pow(c, 2))){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [a, b, c]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }; &nbsp; &nbsp; &nbsp; &nbsp; }; &nbsp; &nbsp; }; &nbsp; &nbsp; return []; }; console.log(almostIsosceles(perimeter));</pre><h2>Output</h2><pre class="result notransalte">[ 105, 104, 181 ]</pre>
Updated on 17-Apr-2021 12:30:10