5

I'm learning JS and I would greatly appreciate your help. I would like to us higher order functions and callbacks if possible.

Task

  1. Declare these local variables and make them equal to the appropriate information, firstName, LastName, linkedIn, phone, city.
  2. Using string concatenation, make a local variable fullName equal to your first and last names separated by a space.
  3. Make a local variable linkedIn, a string which is a link to your LinkedIn profile.
  4. Make a local variable info which is an array containing your fullName, linkedIn, phone, and city variables, in that order.
  5. Make a local variable education which is an array containing the name of your college/university, field of study, an integer which is your year of (anticipated) graduation. Make sure it's in that order.
  6. Define a function createApp which takes your info and education as arguments in that order. This should return an object containing 2 keys. The key names must be the same as your variable names. Set the values of these keys to the corresponding arguments.

Steps 1-5: Created local variables.

 (function person() {
   var firstName = "Rob",
   lastName = "Johnson",
   fullName = firstName + ", " + lastName,
   linkedIn = 'https://www.linkedin.com/in/robjohnson',
   phone = 3105559288,
   city = 'Los Angeles',
   info = [firstName, linkedIn, phone, city],
   education = ['UWRock','Generals','2017'];
 })();

step 6. return an object containing 2 keys. The key names must be the same as your variable names. Set the values of these keys to the corresponding arguments. I have no idea, I assume this should be dynamic not hard coded.

function createApp(info, education){
   var myObj = {}; 
   return(myObj);
};

I think I'm suppose to return something like this using a callback:

myObj {
  info:'Rob', 
  education: ['UWRock','Generals','2017']
};
Robby_rob
  • 190
  • 1
  • 11

2 Answers2

0

Lets say you have following function

var GraphicalFilters = (function() {
    var SelectedFilters = {};     
    return {
        initializaAllFilters: function(value1,value2) {
                    SelectedFilters = {};
                    SelectedFilters.value1 = value1;
                    SelectedFilters.value2 = value2;
        },
        getAllFilters: function() {
            return SelectedFilters;
        }
    };
})();

You can get object via following:

GraphicalFilters.initializaAllFilters(1,2);
var $filters = GraphicalFilters.getAllFilters();
Mahesh
  • 1,063
  • 1
  • 12
  • 29
  • This works, but it doesn't follow the guidelines from the challenge. I have to create a function with two arguments first. Thank you though! – Robby_rob Sep 18 '15 at 15:47
0

I found the solution.

function list () { 
  var firstName = "Rob",
      lastName = "Johnson",
      fullName = firstName + ", " + lastName,
      linkedIn = 'https://www.linkedin.com/in/robjohnson',
      phone = 3105559288,
      city = 'Los Angeles',
      info = [firstName, linkedIn, phone, city],
      education = ['UWRock','Generals','2017'];

  createApp(info,education);

}

function createApp(info, education){
  var emptyObj = {},
      strArr = ['info','education'],
      myArray = [info, education];

      for (var i = 0; i < myArray.length; i++){
        emptyObj[strArr[i]] = myArray[i];
      }

   console.log(emptyObj);

}

list();

The results are

[ Object ] {
 education: ["UWRock", "Generals", "2017"],
 info: ["Rob", "https://www.linkedin.com/in/robjohnson", 3105559288,"Los Angeles"]
}

I'm trying to find a better approach though.

Robby_rob
  • 190
  • 1
  • 11