Help with Redux: Register a Store Listener

Hello! I fail to see why this doesn´t work:

const ADD = 'ADD';

const reducer = (state = 0, action) => {
  switch(action.type) {
    case ADD:
      return state + 1;
    default:
      return state;
  }
};

const store = Redux.createStore(reducer);

// global count variable:
let count = 0;

// change code below this line
function counter(){
  return count + 1
}
store.subscribe(counter)
// change code above this line

store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);

It doesn’t work because count + 1 is not stored anywhere which means that the variable count is not going to be updated and you’ll always have the same output. There are other methods to increment the number and store the result into count each time the number is incremented.

It’s up to you to find how to do that.

1 Like

thank you! Such a silly mistake!

Good Luck & Happy Coding.

I still have troubles understanding why this passes the test:


const reducer = (state = 0, action) => {
  switch(action.type) {
    case ADD:
      return state + 1;
    default:
      return state;
  }
};

const store = Redux.createStore(reducer);

// global count variable:
var count=0;

// change code below this line
function counter(){
  return count ++
}
store.subscribe(counter)
// change code above this line

store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);

and this doesn´t:

const ADD = 'ADD';

const reducer = (state = 0, action) => {
  switch(action.type) {
    case ADD:
      return state + 1;
    default:
      return state;
  }
};

const store = Redux.createStore(reducer);

// global count variable:
var count=0;

// change code below this line
function counter(){
  return count + 1
}
store.subscribe(counter)
// change code above this line

store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);

Any ideas?

1 Like

Is it a parenthesis problem, like:

return(count + 1);

?

Thanks for the reply. No, still won´t budge. I suspect it´s a scope problem, but I honestly don´t know.

I ran the second piece of code, and I figured it out. The console.log statements at the bottom are printing out "0 0 0 " instead of “1 2 3” because you are returning (count + 1), not incrementing the count variable as the instructions say.

This does work:

  return count = count + 1
2 Likes

Thank you! Now I get it.

3 Likes

You should probably mark my last comment as “solution” to close the thread. And you’re welcome!

I already explained why it doesn’t work, you can check my first comment.

As a solution, you can either do as @vipatron suggested: count = count + 1 or do the following, which is easier and cleaner:

count++

(used a lot in for loop).

You can see the lesson again here: Increment a Number with JavaScript.

Also, I hope you didn’t forget that count = count + 1 can be shortened into:

count += 1

You can see the lesson again here: Compound Assignment With Augmented Addition.

And BTW, I just didn’t want to give you the solution so that you think about it and not just get a direct answer and that’s it.

change the return count + 1 to return count += 1 .

1 Like