تابستون داره تموم میشه ها، فرصت‌ها محدودن کلی آفر جذاب در کمپین تابستون🔥👇
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ mahad plus
پاسخ تمرین
جامعه جاوا اسکریپت ایجاد شده در ۱۵ اردیبهشت ۱۴۰۰
// ---------- 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());

سلام

وقت به خیر

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

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

 

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

 

موفق باشید

مهرداد دادخواه ۲۲ اردیبهشت ۱۴۰۰، ۰۱:۲۳