[Solved] Calculate the area of a figure - JS function

  

3
Topic starter

I have to write a JS (JavaScript) function that calculates the area of the figure (see the image below) by given values for w, h, W and H. The lower right corner is always common for the two rectangles.

function to calcualte figure area in javascript

The input comes as four number parameters w, h, W and H.

Examples:

Input:         
2, 4, 5, 3                

Output:
17


Input 2:
13, 2, 5, 8

Output 2:
56

The output should be returned as a result of your function.

1 Answer
2

Here is the code and the solution:

function figureArea(w, h, W, H) {
    var s1 = w * h;
    var s2 = W * H;
    var s3 = Math.min(w, W) * Math.min(h, H);
    //let [s1,s2,s3]=[w * h, W * H, Math.min(w, W) * Math.min(h, H)];
    console.log(s1 + s2 - s3);
}
 
figureArea(13, 2, 5, 8);
Share: