Use getters and setters to Control Access to an Object - FCC

Use getters and setters to Control Access to an Object - FCC
0.0 0

#1

Tell us what’s happening:
All my tests got cleared, but I am not sure whether the code works. When I tried to run code in chrome console, I am getting errors.

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat {
    constructor (temperature){
      this._temperature = temperature;
    }
    get thermos(){
      return this._temperature;
    }
    set thermos(temp){
      this._temperature = (5/9) * (temp - 32);
    }
  }
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object/


#2

What errors are you getting?


#4

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#5

@EdwardHanlon No, the challenge expects you to use the Class syntax.


#6

Yes I see that now sorry


#7

Yes Ariel, Thank you so much for the clear explanation. Here after, I will follow the method of 3 backticks.


#8

I ran your code on chrome console and it works fine…if you try run it again in the same console instance then you get errors as the declaration and instantiation has already taken place and stored in memory.

Dont see any other errors


#9

Thank you @EdwardHanlon


#10

No probkem

Did you have any errors with the import capitalizeString lesson.

Its working for me but the test wont pass