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.
We'll cover the following...
We'll cover the following...
Solution #1: Multiplying Number with Itself
Explanation
The solution to this problem is simple. We multiply the input variable with itself and return the result.
Solution #2: Recursive Method
Explanation
Let’s break the problem down mathematically. We’ll compute the square of a number by direct recursion:
To implement the square operation as a recursive function, we need to first express the square operation in terms of itself:
Mathematically we know that:
...