ผิดตรงไหนครับ ปวดหัวมากครับ 5555555

บอกเราว่าเกิดอะไรขึ้น:
อธิบายปัญหาของคุณโดยละเอียดที่นี่

  **รหัสของคุณ**

class Thermostat{constructor(temp){
this._temp = temp;}
get temperature() {
  return 5/9*(this._temp-32);
  }
set temperature(desiredTemperature) {
  this._temp = desiredTemperature;
  }
}



// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
  **ข้อมูลเบราว์เซอร์ของคุณ:**

ตัวแทนผู้ใช้คือ: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15

Challenge: Use getters and setters to Control Access to an Object

ไปสู่ the challenge:

บทเรียนนี้ต้องลองคิดดีๆครับ อ่านโจทย์ดีๆ และพยายามตีความจากโจทย์ และตัวอย่างการใช้งาน โดยในตอนแรกเราจะเห็นวิธีการใช้งานจาก โค้ดที่มีให้ดังนี้

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

จะเห็นว่าในตอนเรียกใช้ Class ครั้งแรกเขาใส่ argument ที่มีหน่วยเป็น Fahrenheit

const thermos = new Thermostat(76); // Setting in Fahrenheit scale

แต่เมื่อสร้าง instant เสร็จ หน่วยที่ ได้กลับมาเป็น Celsius

let temp = thermos.temperature; // 24.44 in Celsius

ในกรณีนี้เราตั้งสมมุติฐานได้ 2 แบบ คือ มีการเปลี่ยนหน่วยที่ constructor หรือ มีการเปลี่ยนหน่วยตอน get ก็ได้
แต่เมื่อเรามาดูที่ บรรทัดถัดมา

thermos.temperature = 26;

จะเห็นว่าหน่วยที่ set เป็น Celsius ดังนั้น สมมุติฐานว่า มีการเปลี่ยนหน่วยตอน get นั้นจึงตกไป สรุปก็คือเราแค่ต้องเปลี่ยนหน่วย ใน constructor ตามสมมุติฐานที่เหลือครับ

class Thermostat{
  constructor(temp){
      this._temp = 5/9*(temp-32);
     }
    get temperature() {
        return this._temp
    }
    set temperature(desiredTemperature) {
      this._temp = desiredTemperature;
      }
}

ขออนุญาตถามต่อครับ ตอนค่า26เรา เดาหราครับว่าเปน caelsius

ดูจากบรรทัดถัดมาครับ ว่าไม่เปลี่ยนแปลงเลย

ผมอ่านโจทไม่เคลียร์ ลืมอ่านcomment ขอบคุณมากครับ

1 Likes