Javascript Function Creation calculate area via nested function

Description

Javascript Function Creation calculate area via nested function

let area = shapeArea(2, 2, "square");
console.log(area);/*from ww w  .j  av  a 2s .c o m*/

function shapeArea(base, height, shape) {
    let area = 0;
    
    function triangle() {
        area = (base * .5) * height;
    }
    
    function rectangle() {
        area = base * height;
    }

    switch (shape) {
        case "triangle":
            triangle();
            break;
        
        case "rectangle":
            rectangle();
            break;
        
        case "square":
            rectangle();
            break;
    }
    
    return area;
}



PreviousNext

Related