تابستون داره تموم میشه ها، فرصت‌ها محدودن کلی آفر جذاب در کمپین تابستون🔥👇
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ milad karimi
جواب تمرین
جامعه جاوا اسکریپت ایجاد شده در ۰۶ اسفند ۱۴۰۱

سلام و خسته نباشید :

function Person(name , age , heigh , weight){
    this.name = name;
    this.age = age;
    this.heigh = heigh;
    this.weight = weight;
    
   this.sayName = function(){
    return this.name[0].toUpperCase() + this.name.slice(1);
   } 
   this.sayHeight = function(){
     this.heigh = this.heigh / 100;
     return this.heigh + "m";
   }
   this.sayBmi = function(){
    let bm = parseInt(this.weight / (Math.pow(this.weight , 2)));
    return bm;
   }
}
let milad = new Person("milad" , 31 , 185 , 89);
console.log(sayName());
console.log(this.age);
console.log(sayHeight());
console.log(sayBmi());

سلام،

console.log‌ها براساس کدی که نوشتید به این شکل در میاد:

console.log(milad.sayName());
console.log(milad.age);
console.log(milad.sayHeight());
console.log(milad.sayBmi());

در کسر فرمول bmi از height باید استفاده کنید که اشتباها در صورت و مخرج از weight استفاده شده.

بازنویسی کدها براساس class Person:

class Person {
  constructor(name, age, height, weight) {
    this.name = name;
    this.age = age;
    this.height = height;
    this.weight = weight;
  }
  sayName() {
    return this.name.charAt(0).toUpperCase() + this.name.slice(1);
  }
  sayHeight() {
    const heightInMeters = this.height / 100;
    return `${heightInMeters.toFixed(2)}m`;
  }
  sayBmi() {
    const heightInMeters = this.height / 100;
    const bmi = this.weight / Math.pow(heightInMeters, 2);
    return bmi.toFixed(2);
  }
}
بهترین پاسخ
محسن موحد ۰۶ اسفند ۱۴۰۱، ۰۹:۴۴