All files module.js

100% Statements 13/13
100% Branches 10/10
100% Functions 1/1
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 26 27 28 29 30 31 32 33 34 35 36 37                  8x 1x   7x 1x   6x 1x   5x 1x   4x 1x   3x 3x 3x                    
 
 
/**
 * Calculate a person's age in years.
 * 
 * @param {Object} p - An object representing a person, implementing a birth Date parameter.
 * @returns {Number} - The age in years of p.
 */
function calculateAge(p) {
    if (!p) {
        throw new Error("missing param p");
    }
    if (typeof p !== 'object') {
        throw new Error("param not an object");
    }
    if (!p.birth) {
        throw new Error("object has no birth property");
    }
    if (!(p.birth instanceof Date)) {
        throw new Error("birth property is not a Date");
    }
    if (isNaN(p.birth.getTime())) {
        throw new Error("birth property is not a valid Date");
    }
    let dateDiff = new Date(Date.now() - p.birth.getTime());
    let age = Math.abs(dateDiff.getUTCFullYear() - 1970);
    return age;
}
 
 
 
 
 
 
export { calculateAge };