Why do you need to declare a function as a variable again within the function?

Continuing the discussion from freeCodeCamp Challenge Guide: Using Objects for Lookups:

I have updated this question to be more specific to concepts needed to understand coding (see that before the question was like I do not know the answer or where to go at all, please help and now there is a specific theory related concept). My original code is not here but an updated one is (after the instructions) through (a) chance or (b) critical thinking…

These are the instructions:

Using Objects for Lookups
Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to “lookup” values rather than a switch statement or an if/else chain. This is most useful when you know that your input data is limited to a certain range.

Here is an example of a simple reverse alphabet lookup:

var alpha = {
  1:"Z",
  2:"Y",
  3:"X",
  4:"W",
  ...
  24:"C",
  25:"B",
  26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

var value = 2;
alpha[value]; // "Y"

Instructions
Convert the switch statement into a lookup table called lookup. Use it to lookup val and assign the associated string to the result variable.

My understanding is that alpha, bravo, etc. are strings and therefore use bracket notation?
Additionally, to be able to make result = lookup val, since they’re different to begin with, I need to set the values to each other? is it even neccesary to declare a variable if you’ve already declared it as a function?

Follow up question, then: Why create a variable “result”? why not directly return lookup?

I know this seems pedantic…could there be a shorter way to write this code

Here is my original bit of code:

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

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

Instead can’t you do the following (within the function):

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

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

As usual, I will continue to critical think…sometimes I even solve the problem myself after a while by typing it out like I just did. But I think I need help here.

I am still curious about if this answer could be shorter. Do I need to declare var inside the function?

How would you write this as a switch code? I was wondering if this was possible to solve the problem using a switch method (switch, case, case, …default). Is that possible?

var result = "";

switch(val){
  case "alpha":
    result = "Adams";
    break;
  case "bravo":
    result = "Boston"
    break;
  // and all other cases
}

return result;

You wouldn’t want to do that in a real setting though. You would have to write another case for every new property you add. Also, you could not change the values of these properties because you have hard coded them inside a switch statement.

This challenge might help: https://www.freecodecamp.org/challenges/replacing-if-else-chains-with-switch

1 Like

I believe that I don’t have access to that challenge until I complete the javascript segment? Is that correct?

Try clicking the link :slight_smile: You can access all challenges and go through them in any order you want.

It sounds like you need to specify each case for a switch statement. the default part, if you use it, does not work like an “else” part of the if-else statement?

default does work as an else statement.

so either option is a correct way of solving this particular problem? What I’ve learned is that an else can sometimes be a shorter way of writing many cases (like lets say you had 40 cases). If you had 40 if’s, do you think it would be easier to write a switch statement?

Note that you can only use a switch instead of an if, when you are checking one value => switch(value). With an if statement, you can check for anything you like:

if(a == "some_value"){
  // do something
}else if(b == "some_other_value"){
  // do something else
}else{
  // do something
}

You can’t do that with a switch, because you would either have switch(a) or switch(b).

Anyway, even if you have 40 if’s that can be replaced by a switch you would most likely be better of by using an object like they do in the original code you posted.

1 Like

So it sounds like you can use inequality operators (like >) with an if statement but not with a switch?

Or is there a way to use inequality operators with a switch statement?

You could, but that would look weird. Have a look at chosen answer (the switch-range part): https://stackoverflow.com/questions/6665997/switch-statement-for-greater-than-less-than

1 Like