Return result lookup[val

Hi

why does the return with the empty quotes affect the whole code if taken off?
Why does the code not work if I take away the quotes from a property? Because I took it a few lesson back that without quotes would still work?

And why does it not run without the return? Is there an alternative to return?

I got okay but couldnt write the code for the value to be associated with the function. I want to understand it better before moving on.

Thanks


// Setup
function phoneticLookup(val) {
var result = ""

// Only change code below this line
var lookup = {
   "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta":"Denver",
    "echo": "Easy",
   "foxtrot": "Frank",
}

// Only change code above this line
return result = lookup[val]   
}


console.log(phoneticLookup("charlie"));
  **Your browser information:**

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

Challenge: Using Objects for Lookups

Link to the challenge:

Without the return, your function literally returns nothing back to where the function was called. There really isn’t any good alternative to using a return.

I’m not sure what you mean by

why does the return with the empty quotes affect the whole code if taken off?

If you look here, the lookup value is a string, so you will want your lookup table object to also use strings.

Technically, what you pasted in above “should” pass the challenge but I think you will see an error message that says:

" You should not modify the return statement"

The tests are often very strict and don’t like changes where they are not expected. So rework the function without changing the original return statement. It’s basically adding one assignment statement right before the return.

1 Like

Sorry

I actually meant the variable result. It seems that it is necessary part of the code. Yet it is just a variable. However if I set val to result the code works.

Hi Jonny,

Could you please clarify your question? While you’re not meant to modify the result variable above or below the designated editing area, I was still able to pass the tests after doing so and deleting the empty string.

Setting the results variable as an empty string at the top of the function indicates that the variable is intended to be a string. In JavaScript, it’s not necessary to set a variable’s type when it’s initialized. We could also initialize result like this:

var result;

However, it makes it easier to understand what the variable is meant to be used for if we make a reference to its intended type. Since we’re planning to return a string, it makes sense to initialize result as an empty string.

I am also not sure what you meant by the quotation marks being required for the properties. You can still write the object without quotation marks around the keys in the key: value pairs. However, the result is meant to be a string and so the values must be strings.

Here is an example of the function without the limitations you specified that passed the test cases for me:

// Setup
function phoneticLookup(val) {
  var result;
  // Only change code below this line
  var lookup = {
    alpha: "Adams",
    bravo: "Boston",
    charlie: "Chicago",
    delta: "Denver",
    echo: "Easy",
    foxtrot: "Frank"
  }
  result = lookup[val];
  // Only change code above this line
  return result;
}

Hopefully, I was able to clear up some of your questions, but I think I need further detail to fully understand what you’re asking. Please let me know if I can help further!