Using Objects for Lookups Help Me Out Here

I have no idea what I’m doing wrong here, and why I can’t advance. Your input here would be great.

r
Original Problem

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

  // Only change code below this line
  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";
  }

  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("charlie");

Edit


// 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;
}

// Change this value to test
phoneticLookup("charlie");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups

Your function is returning result which equals empty string.

1 Like

What should it look like then?

I don’t know what to assign to result. Why initialize result to empty string then assign result to something? What would your code look like?

Assign a value from the lookup object. val in the function is the name of the property you want to find in your lookup object. How do you access properties in objects? randelldawson mentioned previous challenge that can help you remind this.

The argument phoneticLookup("charlie") is not defined in your switch expresaion to make the function responses a default message if the arguments not found
Use
break; default: result = "the message that you wanna to show" return result }

// 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”
};
var result = lookup[val]
// Only change code above this line
return result;
}

// Change this value to test
phoneticLookup(“charlie”);

Bravo! It works, but you declared result twice.

1 Like