Solution Review: Translate

Detailed discussion on how to solve each problem.

Solution

The solution to this problem relies on the operators available to us and also ensures that properly utilizing all precedence and associative laws. Let’s discuss each question one by one.

Answer 1. Double the sum

The problem was stated as follows:

“For 2 variables, x and y, find twice the value of the sum of the two variables.”

We need to sum the two variables x and y first, and then double the final sum. The solution is below.

Press + to interact
var x=10; // make this equal to var3
var y=20; // make this equal to var1
var ans = 2 * (x+y); // twice the sum of x and y
console.log("Twice of sum of x and y:",ans);

The solution is simple. As shown in line 2, we used (..) for grouping the sum of x and y to make it a single operator. Since (..) has ...