# Function Callback re-factored

• 10-15-2013, 03:34 AM
ab2qik
Function Callback re-factored
Please help. 1st function works but 2nd which is a re-factor of the 1st does not work? Thanks.

//////// 1. WORKS
function addOne(a) { return a + 1; }

function multiplyByTwo(a, b, c, callback) {
var i, myArr = [];
//console.log(arguments)

for (i = 0; i < 3; i++) {
//console.log(callback(arguments[i] * 2 ))
myArr[i] = callback(arguments[i] * 2);
};
return myArr;
}

/////// 2. Does Not WORK
function multiplyByTwo(a, b, c) {
var i, myArr = [];
//console.log(arguments)

for (i = 0; i < 3; i++) {
//console.log(arguments[i] * 2 )
myArr[i] = arguments[i] * 2;
};
return myArr;
}
console.log(multiplyByTwo(1, 2, 3, function (x) {
return x + 1;
}));
• 10-16-2013, 11:40 PM
Logic Ali
Quote:

Originally Posted by ab2qik

/////// 2. Does Not WORK
function multiplyByTwo(a, b, c) {
var i, myArr = [];
//console.log(arguments)

for (i = 0; i < 3; i++) {
//console.log(arguments[i] * 2 )
myArr[i] = arguments[i] * 2;
};
return myArr;
}
console.log(multiplyByTwo(1, 2, 3, function (x) {
return x + 1;
}));

It outputs [2, 4, 6], which is what I would expect.
What were you expecting and why are you passing it a callback that it's not coded to handle?
• 10-17-2013, 05:55 AM
bionoid
Are you trying to create an overloaded function in Javascript? If so then it's not going to work :P

But you could get the "multiplyByTwo" function to support callbacks or not:

Code:

```function addOne(a) {         return a + 1; } function multiplyByTwo(a, b, c, callback) {         var i, myArr = [];         //console.log(arguments)         for (i = 0; i < 3; i++) {                 //console.log(callback(arguments[i] * 2 ))                 myArr[i] = callback ? callback(arguments[i] * 2) : arguments[i] * 2;         }         return myArr; } console.log(multiplyByTwo(1, 2, 3)) console.log(multiplyByTwo(1, 2, 3, addOne)) console.log(multiplyByTwo(1, 2, 3, function (x) {         return x + 2; }));```

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.11709 seconds
• Memory Usage 2,356KB
• Queries Executed 11 (?)
Template Usage (21):
• (1)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates