Use a Switch Statement to Handle Multiple Actions error

Tell us what’s happening:
I am out of options

Your code so far


const defaultState = {
  authenticated: false
};

const authReducer = (state = defaultState, action) => {
  // change code below this line
  //const compare=action.type;
  switch(action.type){
  case 'LOGIN':
     return 
     {
       authenticated: true
       };
    
  case 'LOGOUT':
     return 
     {
       authenticated: false
       };
       
  default:
     return defaultState;
       
      
  }
  // change code above this line
};

const store = Redux.createStore(authReducer);

const loginUser = () => {
  return {
    type: 'LOGIN'
  }
};

const logoutUser = () => {
  return {
    type: 'LOGOUT'
  }
};

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/use-a-switch-statement-to-handle-multiple-actions

I’m thinking the JS engine is interpreting the way you are returning the {authenticated: bool} object as saying return then object which is just going to return undefined, rather than return object.

Try using return(...) if you want to format your code that way, or bump the bracket/entire object up to the same line as return.
Ex: return {authenticated: true}

You are being bitten by a feature of JS called “Automatic Semicolon Inference”. The gist of it is that if a line can be interpreted as a whole statement by itself, it will be. In this case you have this:

return
    somevalue

And what JS is doing is interpreting that as:

    return undefined;  // what a bare 'return', uh, returns
    somevalue;           // we never get here

The solution is what @kylec said: keep the return value on the same line as the return statement. If that clears it up, mark his response as the answer.

1 Like