settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Calculate the area of a figure - JS function

+2 votes

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.

asked in JavaScript category by user golearnweb
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

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);

 

answered by user ak47seo
edited by user golearnweb
...