code-8
code-8

Reputation: 58702

How can I create a helper function in JS?

I'm struggling trying to create a helper function in JS. My function name is updateInfo(X); and it should allow to take in an object.

I have tried

var data = {
    "A": {
        "section_num": "2.2",
        "problem_set": "Same",
        "start_time": "7/20/2015 10:00 am",
        "student_am": "9",
        "due_time": "7/20/2015 11:00 am",
        "submit": "9",
        "avg_score": "71",
        "danger": "5",
        "danger_list": "5,10,15,19,23",
        "warning": "8",
        "warning_list": "3,7,11,13,14,16,21,22",
        "success": "12",
        "success_list": "1,2,4,6,8,9,12,17,18,20,24,25"
    },
    "B": {
        "section_num": "2.3",
        "problem_set": "Not the same",
        "start_time": "6/19/2015 1:00 pm",
        "student_am": "23",
        "due_time": "6/19/2015 2:00 pm",
        "submit": "7",
        "avg_score": "82",
        "danger": "10",
        "danger_list": "11,12,13,14,15,16,17,18,19,20",
        "warning": "10",
        "warning_list": "1,2,3,4,5,6,7,8,9,10",
        "success": "5",
        "success_list": "21,21,23,24,25"
    }
};


//console.log( data.B.section_num ); // <--- HERE I got 2.3

function updateInfo(X) {
    section_num.html(data.X.section_num);
    problem_set.html(data.X.problem_set);
    start_time.html(data.X.start_time);
    student_am.html(data.X.student_am);
    due_time.html(data.X.due_time);
    submit.html(data.X.submit);
    avg_score.html(data.X.avg_score);
    danger.html(data.X.danger);
    danger_list.html(data.X.danger_list);
    warning.html(data.X.warning);
    warning_list.html(data.X.warning_list);
    success.html(data.X.success);
    success_list.html(data.X.success_list);
}

console.log(updateInfo(B)); // < ---HERE I got error...: (

I kept getting

Uncaught ReferenceError: B is not defined

Can someone show me how to fix this ?

What did I forget ?

Here is my fiddle - if you guys need it .

Upvotes: 2

Views: 229

Answers (3)

Rory McCrossan
Rory McCrossan

Reputation: 337627

Firstly, when you call updateInfo you should pass it a string so wrap the value in quotes. The error you see is because JS is looking for a variable named B which is not available in your code.

updateInfo('B');

Then, when you access the properties of the data object you need to provide the key you passed in as a string using bracket notation:

function updateInfo(X) {
    section_num.html(data[X].section_num);
    problem_set.html(data[X].problem_set);
    start_time.html(data[X].start_time);
    student_am.html(data[X].student_am);
    due_time.html(data[X].due_time);
    submit.html(data[X].submit);
    avg_score.html(data[X].avg_score);
    danger.html(data[X].danger);
    danger_list.html(data[X].danger_list);
    warning.html(data[X].warning);
    warning_list.html(data[X].warning_list);
    success.html(data[X].success);
    success_list.html(data[X].success_list);
}

Upvotes: 3

vinayakj
vinayakj

Reputation: 5681

Change

section_num.html(  data.X.section_num  );

and

console.log( updateInfo(B) );

to

section_num.html(  data[X].section_num  );

and

console.log( updateInfo('B') );

The other way could be

function updateInfo(ob){
section_num.html(  ob.section_num  );

and

console.log( updateInfo(data.B) );

Upvotes: 1

Tahir Ahmed
Tahir Ahmed

Reputation: 5737

Try this:

  • Pass B as a string like so: updateInfo("B").
  • And inside the updateInfo function, use it like this: section_num.html(data[X].section_num); ....

Upvotes: 1

Related Questions