پاسخ تمرین

پرسیده شده
فعالیت 1074 روز پیش
دیده شده 437 بار
0
// ---------- class Person ---------
function Person(namePerson, age, height, sex, weight, nationality) {
    this.names = namePerson[0].toUpperCase() + namePerson.substring(1); //convert first Letter to uppercase
    this.age = age;
    this.height = height;
    this.sex = sex;
    this.weight = weight;
    this.nationality = nationality;

    // -------- function to presentation in console --------
    this.presentation = function () {
        let pre = "My name is: " + this.names +"\n"+
                  "My age is: " + this.age +" year\n" +
                  "My height is: " + this.height +" cm\n" +
                  "My sex is: " + this.sex +"\n" +
                  "My weight is: " + this.weight +" kg\n" +
                  "My nationality is: " + this.nationality +"\n" +
                  "My BMI is: " + this.cal_BMI() +"\n" ;
        return pre;
    }
    // -------- function to calculator BMI --------
    this.cal_BMI = function () {
        return Math.round(this.weight / ((this.height/100)**2));  //  BMI = w(kg)/h(m)^2  --> height from cm to m
    }
}
let myPerson = new Person("mohammad", 29, 180, "men", 75, "iranian");
console.log(myPerson.presentation());
فایل پیوست

mahad plus
mahad plus

15 اردیبهشت 00

0
حذف شده

سلام

وقت به خیر

اینکه روی کدهاتون کامنت گذاشتید نکته مثبتی هست. آفرین

تنها موردی که در نگاه اول توجه من را جلب کرد نام تابع محاسبه BMI هست. اسم گذاری توابع به این شکل خیلی استاندارد و مرسوم نیست شما در نام گذاری باید از pascalCase یا CamelCase استفاده کنید که من مدل pascalCase را ترجیح میدم.

 

و یک سوال: آیا نیازه پروپرتی‌های age و ,‘,, به this بایند بشن؟ اگر بشن چه مزیت و چه معایبی داره؟

 

موفق باشید

فایل پیوست

مهرداد دادخواه

توسط

مهرداد دادخواه

22 اردیبهشت 00