0

This is my code:

function makeCounter(x) {
    x = x || 0;
    var obj = {
        value: x,
        increment: function(){
            x = x + 1;
            return x;
        },
        decrement: function() {
            x = x - 1;
            return x;
        }
    }
    return obj;
} 
var counter = makeCounter();

console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 0

Now, I want to know how to edit the code so that the value will be 1 after counter.increment().

6 Answers 6

6

You need to update value after the increase or decrease and use this.value instead of x variable.

function makeCounter(value = 0) {
    return {
        value,
        increment () {
            return ++this.value;
        },
        decrement () {
            return --this.value;
        }
    };
} 

let counter = makeCounter();

console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1

Sign up to request clarification or add additional context in comments.

2 Comments

Good solution, but this needs explanation, too.
Its nothing different then the other. He just shortened it. He is returning the object
1

Update value using this.value & assign value of x to it

function makeCounter(x) {
  x = x || 0;
  var obj = {
    value: x,
    increment: function() {
      x = x + 1;
      this.value = x
      return x;
    },
    decrement: function() {
      x = x - 1;
      this.value = x
      return x;
    }
  }
  return obj;
}
var counter = makeCounter();

console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1

Comments

1

You need to update value after the increase or decrease and use this.value instead of x variable.

You can see my code:

function makeCounter(x) {
    x = x || 0;
    return  {
        value: x,
        increment: function(){
            this.value = this.value + 1;
            return this.value;
        },
        decrement: function() {
            this.value = this.value - 1;
            return this.value;
        }
    }
}
var counter = makeCounter();

console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1
console.log(counter.increment())//output is 2
console.log(counter.value); // output is 2

Comments

1
function makeCounter(x) {
    return {
        value: x || 0,
        increment () {
           // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_() 
            return ++this.value;
        },
        decrement () {
           // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement_(--) 
            return --this.value;
        }
    };
} 

let counter = makeCounter();

console.log(counter.increment()); // output is 1
console.log(counter.value);

Comments

1

In case of nesting, you might fail to use this at some point, you can go with the object's name. as long as you are calling it inside a function

function makeCounter(x) {
    x = x || 0;
    var obj = {
        value: x,
        increment: function(){
            obj.value = obj.value + 1;
            return obj.value;
        },
        decrement: function() {
            obj.value = obj.value - 1;
            return obj.value;
        }
    }
    return obj;
} 
var counter = makeCounter();

console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1

Comments

1

You could use a getter on the object being returned. You get control to do any business logic you like when you do counter.value.

You could change your code like so

function makeCounter(x) {
  x = x || 0;
  var obj = {
    get value() { return x; }, // Only change required
    increment: function() {
      x = x + 1;
      return x;
    },
    decrement: function() {
      x = x - 1;
      return x;
    }
  }
  return obj;
}
var counter = makeCounter();

console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.