Why " var result = ""; "

Tell us what’s happening:
I am able to get past this challenge…the //only change code above this line was a hurdle but thanks to the help got through it.

My question is why do we need the var result = “”; ?
When I remove this statement the function still runs.

Your code so far

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

  // Only change code below this line
  var lookup = {

  return result = lookup[val];

// Change this value to test

I’m a lowbie but I’m trying to learn JS as well and I wanted to try contributing in the FCC forum.
So far, the function shall run whenever you’re going to return something that doesn’t take care of result, otherwise the console will return you that ‘result is not declared’
In this case, you should be able to pass through that by just returning lookup[val] but by storing it into the variable you should be able to use it somewhere else in the function scope.
Anyways, that’s my point, I would still suggest to wait somebody more skilled in that to approve or counter my response. :slight_smile:

1 Like

Kinda what @Rejuvenation said. Also the challenges are quite strict about what you need to put in to pass, so it’s a little bit arbitrary.

The two approaches kinda about how errors are handled. If that function gets an incorrect value:

  1. If it has result intialised as a string (var result = ""), then the lookup will fail, but the function will still return a string.
  2. If it does not, the lookup will fail and an error will be thrown.

In some situations it is better for something to error, in some cases it isn’t; this challenge happens to take the first approach

Appreciate it man, clears things up! Best of luck on the learning.

Thanks for the help! Really clears things up. Seems like a general statement that can be used frequently in many scenarios.