why when i try to get the square root...? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why when i try to get the square root...?

When I tried to get the square root of a 10 in JavaScript I put in my code: let x = 10; console.log(x ** 1/2); when I run the program it says that is equal to 5, but when I write this: let x = 10; let y = 1/2; console.log (x ** y); It shows the really square root (3.1622) Is there a problem with my code? why does it shows 5 in the first code? thank you;

11th Jun 2023, 9:19 PM
Mactus
3 Answers
+ 8
x**(1/2) ** has a higher operator precedence than /
11th Jun 2023, 9:23 PM
Lisa
Lisa - avatar
+ 6
In addition of what Lisa said, i:ll put the example of what you're doing wrong. x**1/2 ** Goes before / So the result is x/2 If x is 9, the result will be 4.5 x**(1/2) (Remember that square root is ** by 1/2) so the result is √x So if x is 9, the result is 3.0 Hope that this helps you to understand what you did wrong
11th Jun 2023, 9:36 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 1
let x = 10; console.log(x ** (1/2));
13th Jun 2023, 6:48 PM
Vaibhav
Vaibhav - avatar