Home > Mobile >  Function to find the square root of a number not working
Function to find the square root of a number not working

Time:09-20

Please be gentle as I have extreme learning difficulties when it comes to maths but I want to try and pass this test on a practice website. The numbers are supplied into the function when it's called but otherwise I've been testing with console.log.

This function should take a number as an argument and return the square of that number. The code I've got below isn't passing all the needs of the test and comes back with the following errors:

Errors:
squareNum's output was 2, but it should be 4
squareNum's output was 9, but it should be 81
squareNum's output was 97, but it should be 9409

Code I have so far:

function squareNum(num){ 
  Math.sqrt
  return num;
} 

Can you please show much working code along with an explanation of why it's working so I can learn from it.

CodePudding user response:

Math.sqrt is a function to find the square root of a number. All you need to do is this:

function squareNum(num) {
    return num * num;
}

CodePudding user response:

From your question, it seems that you need to calculate the square of the number, and not its square root.

The following function should do the trick!

function squareNum(num)
{
    return num*num;
}
  • Related