# Stuck on Redux: Write a Counter with Redux

**URL:** <https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 11, 2018, 3:41pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982 "2018-07-11T15:41:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zootechdrum](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zootechdrum/32/70232_2.png) [@zootechdrum](https://forum.freecodecamp.org/u/zootechdrum)\
**Post date:** [July 11, 2018, 3:41pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/1 "2018-07-11T15:41:50Z")

</div>

```auto
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT'; // define a constant for decrement action types

const defaultState = {
    number: 0
}

const counterReducer = (state = defaultState.number, action) => {
    switch (action.type) {
        case INCREMENT:
            let copy = Object.assign({},defaultState);
                return copy.number + 1;
                break;
        case DECREMENT:
            let copy2 = Object.assign({},defaultState);
                return copy2.number - 1; 
                console.log(defaultState.number) 
                break;   
         default: return state;
    }
   
}

const incAction = function(add) {
    return {
        type:INCREMENT
    }
}

const decAction = function(subtract) {
    return {
        type:DECREMENT
    }
}

const store = Redux.createStore(counterReducer);

```

I have looked at other solutions. I really wanted to make use of Object.assign property as mentioned in the redux documentation. What I am trying to do is make a shallow copy of the default state object and return a new state that will update the number property to the new value.

---

<div class="post-metadata">

**Author:** ![zootechdrum](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zootechdrum/32/70232_2.png) [@zootechdrum](https://forum.freecodecamp.org/u/zootechdrum)\
**Post date:** [July 11, 2018, 3:47pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/2 "2018-07-11T15:47:55Z")

</div>

somehow I managed to pass the test doing this

```auto
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT'; // define a constant for decrement action types

const defaultState = {
    number: 0
}

const counterReducer = (state = defaultState.number, action) => {
    switch (action.type) {
        case INCREMENT:
            let copy = Object.assign({},defaultState);
                return copy.number + 1;
                break;
        case DECREMENT:
            let copy2 = Object.assign({},defaultState);
            console.log(copy2.number-1) //This is the line that I added. 
                return copy2.number //This is also another line that was modified. 
                break;   
         default: return state;
    }
   
}

const incAction = function(add) {
    return {
        type:INCREMENT
    }
}

const decAction = function(subtract) {
    return {
        type:DECREMENT
    }
}

const store = Redux.createStore(counterReducer);

```

what does console.log have to do with the solution to this challenge. Can anyone explain?

---

<div class="post-metadata">

**Author:** ![lubodrinka](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lubodrinka/32/158618_2.png) [@lubodrinka](https://forum.freecodecamp.org/u/lubodrinka)\
**Post date:** [July 11, 2018, 4:31pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/3 "2018-07-11T16:31:44Z")

</div>

semicolon after console missing?

---

<div class="post-metadata">

**Author:** ![zootechdrum](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zootechdrum/32/70232_2.png) [@zootechdrum](https://forum.freecodecamp.org/u/zootechdrum)\
**Post date:** [July 11, 2018, 11:00pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/4 "2018-07-11T23:00:32Z")

</div>

I solved it. Thanks for your input.

---

<div class="post-metadata">

**Author:** ![zootechdrum](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zootechdrum/32/70232_2.png) [@zootechdrum](https://forum.freecodecamp.org/u/zootechdrum)\
**Post date:** [July 11, 2018, 11:24pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/6 "2018-07-11T23:24:27Z")

</div>

i could have sworn it was not passing until I added that line and then it passed. I deleted that line and sure enough it still worked.

I swear I tried it without that line though.

---

<div class="post-metadata">

**Author:** ![sartra](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sartra/32/159791_2.png) [@sartra](https://forum.freecodecamp.org/u/sartra)\
**Post date:** [July 23, 2018, 3:12pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/7 "2018-07-23T15:12:12Z")

</div>

looking at your code and having similar issues. I reworked some things but have questions -  
here is what I have:

```auto
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT'; // define a constant for decrement action types

const defaultState = {
    number: 0
}

const counterReducer = (state = defaultState.number, action) => {
    
    const copy = Object.assign({}, defaultState);
    
    switch (action.type) {
        case INCREMENT:
            return copy.number + 1;
            break;
        case DECREMENT:
            return copy.number; 
            break; 
         default: return state;
    }
}

const incAction = function(add) {
    return {
        type:INCREMENT
    }
}

const decAction = function(subtract) {
    return {
        type:DECREMENT
    }
}

const store = Redux.createStore(counterReducer);

```

So mainly I don’t understand why in the action creator DECREMENT returns copy.number…I would think it should be copy.number - 1;

Also I tried to make copy using ES6 syntax and it doesn’t pass - not sure if I am doing something wrong or it is an issue with FCC ide?

Tried this:

```auto
const copy = {...defaultState} 

```

why shouldn’t that work to make an immutable copy? On stackoverflow I found a post that it should be okay:

> <https://stackoverflow.com/questions/39736397/is-this-a-good-way-to-clone-an-object-in-es6>

---

<div class="post-metadata">

**Author:** ![zootechdrum](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zootechdrum/32/70232_2.png) [@zootechdrum](https://forum.freecodecamp.org/u/zootechdrum)\
**Post date:** [July 27, 2018, 8:27pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/8 "2018-07-27T20:27:50Z")

</div>

Hey did you ever figure it out?

---

<div class="post-metadata">

**Author:** ![simpleasebisees](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/simpleasebisees/32/118913_2.png) [@simpleasebisees](https://forum.freecodecamp.org/u/simpleasebisees)\
**Post date:** [February 28, 2019, 3:25pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/9 "2019-02-28T15:25:40Z")

</div>

Wow, @zootechdrum your code seems to be more mature than mine.

Took me a while to solve it the way I did.  
But some times, I just feel like Freecodecamps Console is very weird. lol.  
Here’s my code, which I used to pass the challenge.

```auto
const INCREMENT = 'INCREMENT'; // define a constant for increment action types
const DECREMENT = 'DECREMENT'; // define a constant for decrement action types

const counterReducer = (state=0, action) => {
  switch(action.type) {
    case INCREMENT:
    return state + 1;
    break;
    case DECREMENT:
    return state - 1;
    break;
    default:
    return state
  }
}; // define the counter reducer which will increment or decrement the state based on the action it receives

const incAction = ()=> {
  return {type:INCREMENT}
}; // define an action creator for incrementing

const decAction = () => {
  return {type:DECREMENT}
}; // define an action creator for decrementing

const store = Redux.createStore(counterReducer); // define the Redux store here, passing in your reducers

```

While I was using the unary operators (state++ and state–) for changing the state, I wasn’t getting it.  
Anyways. Its all good.  
I just admire how you solved the problem with your code, especially within the switch statement.

---

<div class="post-metadata">

**Author:** ![rhowell1](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rhowell1](https://forum.freecodecamp.org/u/rhowell1)\
**Post date:** [March 16, 2019, 12:15am UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/10 "2019-03-16T00:15:22Z")

</div>

Oh my gosh! I was having the same problem and I was using the unary operators for incrementing and decrementing also! But it must have been because I was putting them in the wrong place. I tried prefixing them to return ++state and --state and it passed. It’s gotta be because return state++ will return the value of state before it’s incremented, making it look like it’s not being incremented at all.  
Thank you so much! Your feedback led me to figuring that out.

---

<div class="post-metadata">

**Author:** ![alhermas1981](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alhermas1981/32/124943_2.png) [@alhermas1981](https://forum.freecodecamp.org/u/alhermas1981)\
**Post date:** [October 1, 2019, 3:43am UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/11 "2019-10-01T03:43:31Z")

</div>

```auto
const INCREMENT = 'INCREMENT'; // define a constant for increment action types
const DECREMENT = 'DECREMENT'; // define a constant for decrement action types
const defaultState = {
    number: 0
}
// define the counter reducer which will increment or decrement the state based on the action it receives
const counterReducer = (state = defaultState.number, action) => {
    const copy = Object.assign({}, defaultState);
    switch (action.type) {
        case INCREMENT:
            return copy.number + 1;
        case DECREMENT:
            return copy.number; 
         default: return state;
    }   
} 
// define an action creator for incrementing
const incAction = function(add) {
    return {
        type:INCREMENT
    }
} 
// define an action creator for decrementing
const decAction = function(subtract) {
    return {
        type:DECREMENT
    }
} 
 // define the Redux store here, passing in your reducer
const store = Redux.createStore(counterReducer);

```

---

<div class="post-metadata">

**Author:** ![hsehtak](https://avatars.discourse-cdn.com/v4/letter/h/a6a055/32.png) [@hsehtak](https://forum.freecodecamp.org/u/hsehtak)\
**Post date:** [October 9, 2019, 3:18pm UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/12 "2019-10-09T15:18:36Z")

</div>

why we are not using any dispatch(incAction ) or dispatch (decAction)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 11:58am UTC](https://forum.freecodecamp.org/t/stuck-on-redux-write-a-counter-with-redux/207982/13 "2021-01-25T11:58:32Z")

</div>


