Search⌘ K
AI Features

Solution Review: Compute the Square of a Number

Understand how to compute the square of a number using two approaches: direct multiplication and recursion. Learn the mathematical foundation for the recursive method, how to define base and recursive cases, and implement both solutions in JavaScript.

Solution #1: Multiplying Number with Itself

Javascript (babel-node)
function findSquare(testVariable) {
return testVariable * testVariable;
}
// Driver Code
var testVariable = 5;
console.log(findSquare(testVariable));

Explanation

The solution to this problem is simple. We multiply the input variable with itself and return the result.

...