1

This code is my first attempt to create a module which gives the full name when given the nick name. but I am getting undefined in the results and don't know why. Thanks

let nameProper = (function nameProper (nameShort) {
  let names = {
    "fj": "Fred Johnson"
  };
  return function () {
    return names['nameShort'] || nameShort;
  };
}());

let myName = nameProper('fj');
2
  • names['nameShort'] || nameShort; why are you stringifying nameShort? you probably want names[nameShort] || nameShort; Commented Apr 12, 2016 at 1:12
  • when you use ['nameShort'] with quote, it looks for that string Commented Apr 12, 2016 at 1:12

2 Answers 2

2
const nameProper = (function () {
  const names = {
    fj: "Fred Johnson"
  };

  return function (nameShort) {
    return names[nameShort] || nameShort;
  };
})();

let myName = nameProper('fj');

You need to pass your argument to the inner function, not your closing function that is invoked immediately.

Sign up to request clarification or add additional context in comments.

Comments

0

Alternatively:

let nameProper = function(nameShort)
{
    return this.names[nameShort] || nameShort;
}
.bind
({
    names:
    {
        "fj": "Fred Johnson"
    }
});

let myName = nameProper('fj');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.