Help with Redux: Register a Store Listener

Help with Redux: Register a Store Listener
0

#1

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);

Help with function scope in Redux: Register a Store Listener
#2

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.


#3

thank you! Such a silly mistake!


#4

Good Luck & Happy Coding.


#5

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?


#6

Is it a parenthesis problem, like:

return(count + 1);

?


#7

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


#8

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

#9

Thank you! Now I get it.


#10

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


#11

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.