All files / src areaWithConditionals1.ts

100% Statements 14/14
100% Branches 5/5
100% Functions 4/4
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25    1x 3x     1x 1x       1x 3x     1x 7x 3x 4x 1x 3x 3x      
type Shape = Square | Circle | ShapeArray
 
export class Square {
    constructor (public side: number) {}
}
 
export class Circle {
    constructor (public radius: number) {}
}
 
// represents ncopies of base shape, arranged in a row
export class ShapeArray {
    constructor (public base: Shape, public ncopies: number) {}
}
 
export function area (s:Shape) : number {
    if (s instanceof Square) {
        return s.side * s.side
    } else if (s instanceof Circle) {
        return Math.PI * s.radius * s.radius
    } else if (s instanceof ShapeArray) {
        return s.ncopies * area(s.base)
    }
}