Languages
[Edit]
PL

JavaScript - jak obliczy─ç pierwiastek dowolnego stopnia?

1 points
Created by:
Sylwia
1337

Korzystaj─ůc z JavaScript mo┼╝na obliczy─ç pierwiastek dowolnego stopnia w nast─Öpuj─ůcy spos├│b.

1. Math.pow przykład metody

// ONLINE-RUNNER:browser;

function calculateRoot(value, degree) {
    return Math.pow(value, 1.0 / degree);
}

// Examples:

// degree = 2
console.log( calculateRoot(  9  , 2 ) ); //  3
console.log( calculateRoot(  4  , 2 ) ); //  2

console.log( calculateRoot(  2  , 2 ) ); //  1.4142135623730951
console.log( calculateRoot(  0.5, 2 ) ); //  0.7071067811865476
console.log( calculateRoot(  0  , 2 ) ); //  0
console.log( calculateRoot( -1  , 2 ) ); //  NaN

// degree = 3
console.log( calculateRoot(  8  , 3 ) ); // 2
console.log( calculateRoot(  27 , 3 ) ); // 3

console.log( calculateRoot(  2  , 3 ) ); // 1.2599210498948732
console.log( calculateRoot(  0.5, 3 ) ); // 0.7937005259840998
console.log( calculateRoot(  0  , 3 ) ); // 0 
console.log( calculateRoot( -1  , 3 ) ); // NaN

// float degree
console.log( calculateRoot(  25 ,  0.5   ) ); // 625
console.log( calculateRoot(  25 , -0.5   ) ); // 0.0015999999999999999
console.log( calculateRoot(  25 ,  0.005 ) ); // 3.872591914849318e+279

 

Hey ­čĹő
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.