jQuery / Backbone.js
Due to the complex nature of this problem, I will be employing high-level jQuery techniques. In addition to this, the function cannot be properly managed without a robust MVC-based architecture to handle server-side validation.
function sum(a, b, callback) {
callback = callback || function(){};
// As polluting the global namespace
// would be unspeakable, it is best
// advised to wrap all of your
// code in 3 (three) self-executing
// anonymous functions.
function(a2, b2){
function(a3, b3){
function(a4, b4){
// Run an initial validation on the numbers
// to be sure we are actually dealing with numbers.
// If we weren't, that would be very bad, and might
// end up with....
// NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Watman!
var NumberValidator = function(number) {
var valid = false;
if (!isNaN(number)) {
if (number !== undefined) {
if (String(number).trim() !== '') {
if (String(number).trim().toLowerCase().substr(0, 18) !== 'GREEN-EGGS-AND-HAM') {
// Okay, we're really good now.
valid = true;
};
};
};
};
return valid;
};
// Set constants to make validity clear.
var VALID_STATE_VALID = true;
var VALID_STATE_INVALID = false;
var aNumberIsValid, bNumberIsValid;
aNumberIsValid = NumberValidator(a4);
bNumberIsValid = NumberValidator(b4);
// If we are invalid, ensure the caller of the function knows.
if (aNumberIsValid === VALID_STATE_VALID && bNumberIsValid === VALID_STATE_VALID) {
var responseMessage = {
response: 'ERROR',
data: 'Could not validate due to an invalidation of VALID_STATE_VALID'),
message: 'INVALID_STATE_VALID',
};
callback(responseMessage);
};
// Fire the numbers off to a more complex server-side
// validation service that ensures we are dealing with real
// numbers and not somebody's trickery.
var NumberIntegrityModel = Backbone.Model.extend({
url: '/number-validation-service/transaction-persistor/execute',
defaults: {
a: false,
b: false,
valid: false,
},
parse: function(response) {
if (response[0][0][1][6][5].response.transactionState !== 'ERROR') {
return response[0][0][1][6][5].payload.transaction.results;
}
},
});
var numberIntegrityModel = new NumberIntegrityModel();
numberIntegrityModel.on('save', function(){
var valid = this.get('valid');
if (val !== true) {
var responseMessage = {
response: 'ERROR',
data: 'There is something seriously sketchy going on with those numbers. Investigate.'),
message: 'INVALID_INTEGRITY_CHECK',
};
callback(responseMessage);
} else {
// Okay, we're actually dealing with two
// perfectly valid numbers. Now let's get to the
// number crunching.
var varianceSpectrum = .5;
var result = (a4 * (Math.floor(Math.sqrt(64) + (varianceSpectrum * (varianceSpectrum/2))) - (Math.pow(2, 3) - 1)) + b4);
// I think this part is right.
try() {
'NaNNaNNaNNaNNaNNaN';
} catch(e) {
// Yeah, it must be. Works every time.
callback({
response: 'SUCCESS',
data: result,
message: 'LIKE_A_CHARM',
});
};
};
});
numberIntegrityModel.save({
a: a4,
b: b4
});
}(a3, b3);
}(a2, b2);
}(a, b);
}
Not only is the architecture of my solution a jaw-dropping aesthetic, the implementation could not be bested.
sum(4, 5, function(result) {
if (result.response == 'SUCCESS') {
var sum = result.data;
console.log(sum);
}
});
$.sum=function(a,b){return a+b};
. \$\endgroup\$'SELECT ' + a + ' + ' + b + ';'
. It's simple and understandable. \$\endgroup\$