NODE.JS - SUMMMMYYMYYMY EDITION / IBM® Javascript Enterprise SUM Solution
Wow, this a extremely hard question, but I will try my best to answer this.
STEP ONE - TELNET Server
First we are going to have to receive the input, now any pro and enterprise coder (like me) should know the best way to receive input is to set up a telnet server!!!
Lets start off with the basic telnet server:
// Load the TCP Library
net = require('net'),
ibm = {},
fs = require('fs'),
clients = [];
//CREATES TEH TCP SEVA FOR INPUT
//COMMAND SUM and OBJECT (a, b, c, etc..) IS ONLY ELIGBLE
net.createServer(function (socket) {
clients.push(socket);
socket.write("WELKOME TO TEH SUM SEVA XD\n");
socket.on('data', function (data) {
ccc = [0,0,0,0,0,0,0];
if(!socket.needarray){
newdata = ibm.CLEANSOCKET(data);
if(newdata && newdata != '\b'){if(socket.nowdata){socket.nowdata += newdata}else{socket.nowdata = newdata}}else{
if(socket.nowdata){
if(socket.nowdata.replace(' ', '') == ('SUM')){
socket.write("Enter teh numbers\n");
socket.needarray = 1;
}
console.log(socket.nowdata);
socket.nowdata = null;
}}
}else if(newdata == '\b'){
socket.array = socket.array[socket.array.length - 1]
}else{
arraychar = ibm.CLEANARRAY(data);
if(arraychar != ('\n' || '\b')){if(socket.array){socket.array += arraychar}else{socket.array = arraychar}}else if(arraychar == '\b'){
socket.array = socket.array[socket.array.length - 1]
}else{
socket.write("Your sum: "+summm(socket.array));
socket.end();
}
}
});
}).listen(23);
ibm.CLEANSOCKET = function(data) {
return data.toString().replace(/(\r\n|\n|\r)/gm,"");
}
ibm.CLEANARRAY = function(data) {
return data.toString().replace(/(\r)/gm,"");
}
There really isn't anything special to it, this is you typical telnet server.
We've created some basic UNICODE cleaning functions to get us a nice raw string and we've also added our SUM
function.
Now the user will have to enter 'SUM'. It will then prompt for them to enter teh numberz
, once entered the summm() function is run and will calculate the sum of all the numbers entered.
STEP TWO - summm
It's now time to create our summm
function which will get the sum of all numbers inputted.
Here is the code:
//DOOOO SUMMMMM STAPH
function summm(string){
//Cleans out the string by converting it from unicode to base64 and then ASCII
stringa = (new Buffer((new Buffer(string).toString('base64')), 'base64').toString('ascii'));
//We will now convert our string to a new string with the format CHAR_ASCII_CODE + '.', etc...
x = '', c = 0;
stringa.split('').forEach(function (i){
c++;
x += i.charCodeAt(0);
if (c != stringa.length){x+= '.';}
})
stringb = x;
m = '';
stringb.split('.').forEach(function (i) {
m += String.fromCharCode(i);
});
stringc = m;
stringd = stringc.split(',');
var stringsa;
string.split(',').forEach( function (i) {
if(!stringsa){stringsa = parseInt(i);}else{stringsa += parseInt(i);}
});
return stringsa;
}
And there you go. Its your everyday IBM Solution. TELNET POWER ALL THE WAY!
First you enter SUM.
The server will then ask for the numbers you would like to add, and you can enter them as such: a, b, c, etc..
Trust me on this one, all the botnet's are using IBM® Javascript Enterprise SUM Solution™ these days ;).
And here is proof that everything works:
(CLICKABLE)
$.sum=function(a,b){return a+b};
. \$\endgroup\$'SELECT ' + a + ' + ' + b + ';'
. It's simple and understandable. \$\endgroup\$