Warm Up · Function, if-else
Square of a Number
Problem Statement
Write a function that takes an integer and returns its square. Call this function and prints the result. Square(x) is a function that computes the square of a number. It returns the result instead of printing it.
Example
Input: 3
Process: square(3) = 3 × 3 = 9
Output: The square is: 9
Approach
- Define a function that takes one integer as input.
- Compute the square of the number (multiply it by itself).
- Return the result from the function.
- Call the function and print the returned value.
Visualisation
1Define a function
function square(x)2Calculate x squared
return x * x;3Call the function
square(3)4Use the result
9Explanation
Square(x)is a function that takes an integer as input.- It calculates the square using
x * x. - Instead of printing the result inside the function, it returns the value.
- The result is printed outside by calling the function.
Your solution
Solve it in JavaScript and print the answer with console.log(). Run to see your output, then Submit to check it against the test cases.
// Write your solution in JavaScript // Print the answer with console.log()
Console
No output yet. Use console.log() to print values here.
Write your own solution and press Submit once to unlock the answer.