Saturday 28 November 2020

JavaScript - Računanje hipotenuze na osnovu Pitagorine teoreme

ZADATAK: Napisati kod koji će računati dužinu hipotenuze (c) trougla na osnovu Pitagorine teoreme. Obratiti pažnju da unos korisnika za ostale dvije stranice (a i b) mora biti isključivo broj i veći od 0. U suprotnom ispisati grešku i vrtiti petlju sve dok unosi za stranice a i b ne budu zadovoljeni.

RJEŠENJE:


PRIMJER:

Unos dužine stranice a:

 


Unos dužine stranice b:


 

Rezultat:

    


 

KOD:
https://paste.ofcode.org/VaadND5s8WUxvtLcUjwGrd

  1. <script>
  2. function pTeorema (a=2, b=2)
  3. {
  4. var a, b, c;
  5. a = prompt("Unesite dužinu stranice a:");
  6. b = prompt("Unesite dužinu stranice b:");
  7.  
  8. while (a < 1 || b < 1 || isNaN(a) || isNaN(b)) {
  9. alert("Obje stranice moraju biti broj i veće od 0!");
  10. a = prompt("Unesite dužinu stranice a:");
  11. b = prompt("Unesite dužinu stranice b:");
  12. }
  13. c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)).toFixed(2);
  14. return c;
  15. }
  16. document.write("Dužina hipotenuze je: " + pTeorema());
  17. </script>

 

 


Categories:

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.