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);
}
});
31You can use my lightweight jQuery plugin:
$.sum=function(a,b){return a+b};
. – Blender – 2013-12-28T13:51:04.5705I knew I'd get a jQuery reference sometime – scrblnrd3 – 2013-12-28T13:51:41.270
If you don't use jQuery, you don't know programming. – None – 2013-12-28T17:30:34.500
5Brilliant English :p – Martijn Courteaux – 2013-12-28T17:39:22.110
5Question suggestion (not sure if it's any good): "GUISE HALP, I need a fast algorithm to generate bitcoin blocks!!!!! It's super urgent!" – None – 2013-12-28T18:42:38.860
Some people are using subtraction in their answers below. But, if we allow use of the binary
-
, then suddenly the OP should realize that an "ok" solution is:result = a - (-b)
, baffling our obfuscation attempts. – apnorton – 2013-12-28T19:21:25.1475These answers are quite involved. I suggest opening a connection to your database and issuing
'SELECT ' + a + ' + ' + b + ';'
. It's simple and understandable. – Nick Chammas – 2013-12-28T20:57:58.757Is the trolled solution supposed to return the correct value? – jscs – 2013-12-28T21:00:12.390
@JoshCaswell It is. However, the correct answer is up to your interpretation. Goal of these code-trolling tagged questions is to give hilarious and hopefully useless answers that could still be marked as accepted. For more info, follow the linked Wiki article the OP gave... – Kiruse – 2013-12-28T23:25:23.873
Is there already a solution using AppleScript to call Spotlight to do the operation? – o0'. – 2013-12-29T13:49:26.537
I personally thought the english in the original was quite fitting... – Camilo Martin – 2013-12-29T20:36:38.493
I thought so too – scrblnrd3 – 2013-12-29T20:53:36.103
Damn – too late. I had a solution using bitcoin-wallets. – Fabian Zeindl – 2014-01-03T19:57:30.063
@FabianZeindl well you can post it, you might get some votes – scrblnrd3 – 2014-01-03T20:29:42.867
Write a python script that can generate another python script which contains a function doing something like
if args == [1,2]: return 3
. – yegle – 2014-02-17T01:16:46.020(C#) ```int Sum (ref int a, ref int b) { var l = new List<int>() { a, b, a + b }; l.Sort((i, j) => random.Next(-1, 1));
a = l[0]; b = l[1]; return l[2]; }``` You will never know where I will return the sum. – Fabricio – 2014-05-07T15:52:33.067
Code-trolling is in the process of being removed, as per the official stance. This question is very highly voted with many answers, many of which are highly voted, and even though it recieved over 50% "delete" votes on the poll, it only did by about 12%. Therefore, I am locking it for historical significance.
– Doorknob – 2014-05-11T22:35:40.593