Covert `switch` to `object`

This is the Using Objects for Lookups JavaScript challenge.

Instructions say: Convert the switch statement into an object called lookup. Use it to look up val and assign the associated string to the result variable

Here’s the starter code:

function phoneticLookup(val) {
  var result = "";

  switch(val) {
    case "alpha":
      result = "Adams";
      break;
    case "bravo":
      result = "Boston";
      break;
    case "charlie":
      result = "Chicago";
      break;
    case "delta":
      result = "Denver";
      break;
    case "echo":
      result = "Easy";
      break;
    case "foxtrot":
      result = "Frank";
  }
  return result;
}
phoneticLookup("charlie");

Here’s my solution which passed.

function phoneticLookup(val) {
  var result = ""; //result variable
  var lookup = { //switch converted to lookup object with key: value pairs
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
  };
  return lookup[val]; //return value of key
  return result; //return result variable so function call returns correct value of key
}
console.log(phoneticLookup("charlie")); // key is called and logs Chicago

I’ve included comments in my solution because I want to make sure that I’ve correctly identified the purpose of each code snippet.
Thanks

Hey @Mike6,

Do you have any problem with the challenge? You did not specify what you’re having trouble with. Please don’t just show your solutions.

Do you have a question about your solution?

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like

false, after a return statement is executed the function stops and whatever is after is not executed anymore, the function stops at previous line

1 Like

All your comments are right except for your second return statement, which will never be reached.

Right because even though the early return was incorrect, it didn’t throw an error.

I changed:
return lookup[val]; //return value of key
return result; //return result variable so function call returns correct value of key

to:
result = lookup[val]; //assigns value of key to result variable
return result; //return result variable so function call returns correct value of key
Thanks

1 Like

Right because even though the early return was incorrect, it didn’t throw an error.

I changed:
return lookup[val]; //return value of key
return result; //return result variable so function call returns correct value of key

to:
result = lookup[val]; //assigns value of key to result variable
return result; //return result variable so function call returns correct value of key
Thanks

That’s still not correct. Nothing happens inside a function after return. Absolutely nothing. Your second return should have the comment // Never executes.

Since I changed the code, what was the “second” return is now the only return

Ah, now I see. It’s hard to read code inline like that:

your code

easier to read code

This is still wrong in the sense that it does not do what the original code was doing.

The original codes first sets result to an empty string. Then updates the result to the correct value inside the switch and finally returns the result.

The logic in the above is that when the passed in val is not found in the switch the function will return the empty string.

Your code does not handle this case (where the val is not in the lookup).
You function now returns undefined instead of an empty string, for this case.