جواب تمرین ساختن Object با Constructor Functions

پرسیده شده
فعالیت 894 روز پیش
دیده شده 497 بار
0

سلام.  دو ساختار به صورت مجزا نوشتم. BMI برای افراد زیر ۲ سال فرمول متفاوتی داشت به همین دلیل این Object فقط برای افراد بالا یا برابر ۲ سال هست و بررسی نوع داده ها رو خودکار کردم و یک سری محدودیت برای ورودی ها گذاشتم. اگر مشکلی یا نکته ای در رابطه با این کد هست ممنون میشم بهم اطلاع بدید و همینطور روش رفعش رو.

/**
 * Create a person object for age older than 2 or equal with 2
 * @description age, height and weight params should be integer numbers
 *              between first and second number or equal with those numbers.
 * @param {string} name FullName - String between 3 and 100 characters or equal with 0 or 100
 * @param {number} age Just Year -  2 and 250
 * @param {number} height in CM - 20 and 500
 * @param {number} weight in KG - 1 and 1000
 * @returns object
 */
function createPerson(name, age, height, weight) {
    // Checking params Data Types and Limitations.
    let params = [
        ['name', name, 'string', 3, 100],
        ['age', age, 'number', 2, 250],
        ['height', height, 'number', 20, 500],
        ['weight', weight, 'number', 1, 1000]
    ];
    let paramsLength = params.length;

    // 
    for(let paramNum = 0; paramNum < paramsLength; paramNum++) {
        if(params[paramNum][1] !== undefined || params[paramNum][1] === null) {
            if(params[paramNum][2] == 'number') {
                if(typeof params[paramNum][1] != params[paramNum][2] || params[paramNum][1] != parseInt(params[paramNum][1])) {
                    throw TypeError(`The ${params[paramNum][0]} parameter must be a value of the Number data type and an integer number.`);
                } else if(params[paramNum][1] < params[paramNum][3] || params[paramNum][1] > params[paramNum][4]) {
                    throw TypeError(`${params[paramNum][0]} parameter must be between ${params[paramNum][3]} and ${params[paramNum][4]} or equal with ${params[paramNum][3]} or ${params[paramNum][4]}.`);
                }
            } else {
                if(typeof params[paramNum][1] != params[paramNum][2] || params[paramNum][1] == '') {
                    throw TypeError(`The ${params[paramNum][0]} parameter must be a value of the String data type and not empty.`);
                } else if(params[paramNum][1].length < params[paramNum][3] || params[paramNum][1].length > params[paramNum][4]) {
                    throw TypeError(`${params[paramNum][0]} parameter must be between ${params[paramNum][3]} and ${params[paramNum][4]} or equal with ${params[paramNum][3]} or ${params[paramNum][4]}.`);
                }
            }
        } else {
            params[paramNum][1] = null;
        }
    }

    // Creating person object with edited params
    let person = {};
        person.name = (typeof params[0][1] == 'string') ? params[0][1][0].toUpperCase() + params[0][1].slice(1) : null;
        person.age = params[1][1];
        person.height = params[2][1];
        person.weight = params[3][1];
        person.sayName = function () {
            if(typeof person.name != 'string' || person.name == '') {
                throw TypeError('Value of name param is not set or is equal with empty string.');
            } else {
                console.log('My name is ' + person.name);
            }
        };
        if(typeof person.weight == 'number' && typeof person.height == 'number') {
            person.bmi = parseInt(person.weight / (Math.pow((person.height / 100).toFixed(2), 2))); // Converting height from CM to M and Calculating BMI | Other Formulas for height^2 - height * height or for new versions of JS (height ** 2);
        } else {
            person.bmi = null;
        }
    // Returning created Person object
    return person;
}

/**
 * Create a person object for age older than 2 or equal with 2
 * @description age, height and weight params should be integer numbers
 *              between first and second number or equal with those numbers.
 * @param {string} name FullName - String between 3 and 100 characters or equal with 0 or 100
 * @param {number} age Just Year -  2 and 250
 * @param {number} height in CM - 20 and 500
 * @param {number} weight in KG - 1 and 1000
 * @returns object
 */
 function CreatePerson2(name, age, height, weight) {
    // Checking params Data Types and Limitations.
    let params = [
        ['name', name, 'string', 3, 100],
        ['age', age, 'number', 2, 250],
        ['height', height, 'number', 20, 500],
        ['weight', weight, 'number', 1, 1000]
    ];
    let paramsLength = params.length;

    // 
    for(let paramNum = 0; paramNum < paramsLength; paramNum++) {
        if(params[paramNum][1] !== undefined || params[paramNum][1] === null) {
            if(params[paramNum][2] == 'number') {
                if(typeof params[paramNum][1] != params[paramNum][2] || params[paramNum][1] != parseInt(params[paramNum][1])) {
                    throw TypeError(`The ${params[paramNum][0]} parameter must be a value of the Number data type and an integer number.`);
                } else if(params[paramNum][1] < params[paramNum][3] || params[paramNum][1] > params[paramNum][4]) {
                    throw TypeError(`${params[paramNum][0]} parameter must be between ${params[paramNum][3]} and ${params[paramNum][4]} or equal with ${params[paramNum][3]} or ${params[paramNum][4]}.`);
                }
            } else {
                if(typeof params[paramNum][1] != params[paramNum][2] || params[paramNum][1] == '') {
                    throw TypeError(`The ${params[paramNum][0]} parameter must be a value of the String data type and not empty.`);
                } else if(params[paramNum][1].length < params[paramNum][3] || params[paramNum][1].length > params[paramNum][4]) {
                    throw TypeError(`${params[paramNum][0]} parameter must be between ${params[paramNum][3]} and ${params[paramNum][4]} or equal with ${params[paramNum][3]} or ${params[paramNum][4]}.`);
                }
            }
        } else {
            params[paramNum][1] = null;
        }
    }

    // Creating person object with edited params
    this.name = (typeof params[0][1] == 'string') ? params[0][1][0].toUpperCase() + params[0][1].slice(1) : null;
    this.age = params[1][1];
    this.height = params[2][1];
    this.weight = params[3][1];
    this.sayName = function () {
        if(typeof person.name != 'string' || person.name == '') {
            throw TypeError('Value of name param is not set or is equal with empty string.');
        } else {
            console.log('My name is ' + person.name);
        }
    };
    if(typeof this.weight == 'number' && typeof this.height == 'number') {
        this.bmi = parseInt(this.weight / (Math.pow((this.height / 100).toFixed(2), 2))); // Converting height from CM to M and Calculating BMI | Other Formulas for height^2 - height * height or for new versions of JS (height ** 2);
    } else {
        this.bmi = null;
    }
}

let person2 = new CreatePerson2('Person 2', 24, 180, 80);
console.log(person2);
فایل پیوست

قاسمی
قاسمی

13 آبان 00