Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

JavaScript (213 characters compacted, 133 if I'm willing to sacrifice my soul)

For clarity, this is the code before I compacted it down (JS-Fiddle of it):

function math(match, leftDigit, operator, rightDigit, offset, string) {
    var L = parseInt(leftDigit)
    var R = parseInt(rightDigit)
    switch (operator)
    {
        case '*': return R*L;
        case '\/': return R/L;
        case '+': return L+R;
        case '-': return L-R;
    }
};

multAndDivRegex = /(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/; //Right to left
addAndSubRegex = /(\-?\d+)([\+\-])(\-?\d+)/; // Left to right

while(multAndDivRegex.test(str)) {
    str = str.replace(multAndDivRegex, math);
}

while(addAndSubRegex.test(str)) {
    str = str.replace(addAndSubRegex, math);
}

Here it is compacted down to 213 characters (containing some extra help from Toni AlmeidaToni Almeida) (JS-Fiddle of it):

function m(x,l,o,r){
    L=~~l;R=~~r;
    return o=='*'?R*L:o=='/'?R/L:o=='+'?L+R:L-R
}

for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));
for(A=/(\-?\d+)([\+\-])(\-?\d+)/;A.test(r=s);s=s.replace(A,m));

Since I'm ending lines with semi-colons, all removable whitespace was ignored for character counting, but left in for clarity.

I can get it down to 133 characters if I use eval(), but that's not how I roll... (JS-Fiddle)

function m(x,l,o,r){return o=='*'?~~r*~~l:~~r/~~l}for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));r=eval(s)

This solution would mean that people can enter ANY JavaScript code they want! The 303 character version does not execute any JavaScript; it only does math.

JavaScript (213 characters compacted, 133 if I'm willing to sacrifice my soul)

For clarity, this is the code before I compacted it down (JS-Fiddle of it):

function math(match, leftDigit, operator, rightDigit, offset, string) {
    var L = parseInt(leftDigit)
    var R = parseInt(rightDigit)
    switch (operator)
    {
        case '*': return R*L;
        case '\/': return R/L;
        case '+': return L+R;
        case '-': return L-R;
    }
};

multAndDivRegex = /(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/; //Right to left
addAndSubRegex = /(\-?\d+)([\+\-])(\-?\d+)/; // Left to right

while(multAndDivRegex.test(str)) {
    str = str.replace(multAndDivRegex, math);
}

while(addAndSubRegex.test(str)) {
    str = str.replace(addAndSubRegex, math);
}

Here it is compacted down to 213 characters (containing some extra help from Toni Almeida) (JS-Fiddle of it):

function m(x,l,o,r){
    L=~~l;R=~~r;
    return o=='*'?R*L:o=='/'?R/L:o=='+'?L+R:L-R
}

for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));
for(A=/(\-?\d+)([\+\-])(\-?\d+)/;A.test(r=s);s=s.replace(A,m));

Since I'm ending lines with semi-colons, all removable whitespace was ignored for character counting, but left in for clarity.

I can get it down to 133 characters if I use eval(), but that's not how I roll... (JS-Fiddle)

function m(x,l,o,r){return o=='*'?~~r*~~l:~~r/~~l}for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));r=eval(s)

This solution would mean that people can enter ANY JavaScript code they want! The 303 character version does not execute any JavaScript; it only does math.

JavaScript (213 characters compacted, 133 if I'm willing to sacrifice my soul)

For clarity, this is the code before I compacted it down (JS-Fiddle of it):

function math(match, leftDigit, operator, rightDigit, offset, string) {
    var L = parseInt(leftDigit)
    var R = parseInt(rightDigit)
    switch (operator)
    {
        case '*': return R*L;
        case '\/': return R/L;
        case '+': return L+R;
        case '-': return L-R;
    }
};

multAndDivRegex = /(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/; //Right to left
addAndSubRegex = /(\-?\d+)([\+\-])(\-?\d+)/; // Left to right

while(multAndDivRegex.test(str)) {
    str = str.replace(multAndDivRegex, math);
}

while(addAndSubRegex.test(str)) {
    str = str.replace(addAndSubRegex, math);
}

Here it is compacted down to 213 characters (containing some extra help from Toni Almeida) (JS-Fiddle of it):

function m(x,l,o,r){
    L=~~l;R=~~r;
    return o=='*'?R*L:o=='/'?R/L:o=='+'?L+R:L-R
}

for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));
for(A=/(\-?\d+)([\+\-])(\-?\d+)/;A.test(r=s);s=s.replace(A,m));

Since I'm ending lines with semi-colons, all removable whitespace was ignored for character counting, but left in for clarity.

I can get it down to 133 characters if I use eval(), but that's not how I roll... (JS-Fiddle)

function m(x,l,o,r){return o=='*'?~~r*~~l:~~r/~~l}for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));r=eval(s)

This solution would mean that people can enter ANY JavaScript code they want! The 303 character version does not execute any JavaScript; it only does math.

Add EVEN shorter solution
Source Link
IQAndreas
  • 1.2k
  • 6
  • 11

JavaScript (303213 characters compacted, 204133 if I'm willing to sacrifice my soul)

Here it is compacted down to 303213 characters (JS-Fiddle of itcontaining some extra help from Toni Almeida) (JS-Fiddle of it):

function m(x,l,o,r){
    L=parseInt(l);
    R=parseInt(r);
    switch (o){L=~~l;R=~~r;
        case '*': return R*L;
        case '\o=='*'?R*L:o=='/': return ?R/L;
        case '+'L: return L+R;
        case '-'o=='+'?L+R: return L-R;
    }R
};

s=promptfor();
M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));
for(A=/(\-?\d+)([\+\-])(\-?\d+)/;
while(M.test(s)){s=s.replace(M,m)};
while(A;A.test(s)r=s){s=s;s=s.replace(A,m)};
alert(s);

I can get it down to 204133 characters if I use eval(), but that's not how I roll... (JS-FiddleJS-Fiddle)

function m(x,l,o,r){L=parseInt(l);R=parseInt(r);switch(o){case '*':return R*L;case '\/'o=='*'?~~r*~~l:return R~~r/L;}~~l};s=promptfor();M=M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;while(M;M.test(s)){s=s;s=s.replace(M,m)};alert(eval);r=eval(s))

JavaScript (303 characters compacted, 204 if I'm willing to sacrifice my soul)

Here it is compacted down to 303 characters (JS-Fiddle of it):

function m(x,l,o,r){
    L=parseInt(l);
    R=parseInt(r);
    switch (o){
        case '*': return R*L;
        case '\/': return R/L;
        case '+': return L+R;
        case '-': return L-R;
    }
};

s=prompt();
M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;
A=/(\-?\d+)([\+\-])(\-?\d+)/;
while(M.test(s)){s=s.replace(M,m)};
while(A.test(s)){s=s.replace(A,m)};
alert(s)

I can get it down to 204 characters if I use eval(), but that's not how I roll... (JS-Fiddle)

function m(x,l,o,r){L=parseInt(l);R=parseInt(r);switch(o){case '*':return R*L;case '\/':return R/L;}};s=prompt();M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;while(M.test(s)){s=s.replace(M,m)};alert(eval(s))

JavaScript (213 characters compacted, 133 if I'm willing to sacrifice my soul)

Here it is compacted down to 213 characters (containing some extra help from Toni Almeida) (JS-Fiddle of it):

function m(x,l,o,r){
    L=~~l;R=~~r;
    return o=='*'?R*L:o=='/'?R/L:o=='+'?L+R:L-R
}

for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));
for(A=/(\-?\d+)([\+\-])(\-?\d+)/;A.test(r=s);s=s.replace(A,m));

I can get it down to 133 characters if I use eval(), but that's not how I roll... (JS-Fiddle)

function m(x,l,o,r){return o=='*'?~~r*~~l:~~r/~~l}for(M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;M.test(s);s=s.replace(M,m));r=eval(s)
Add 204 char answer
Source Link
IQAndreas
  • 1.2k
  • 6
  • 11

JavaScript (303 characters compacted, 204 if I'm willing to sacrifice my soul)

Since I'm ending lines with semi-colons, all removable whitespace was ignored for character counting, but left in for clarity.

I can get it down to 204 characters if I use eval(), but that's not how I roll... (JS-Fiddle)

function m(x,l,o,r){L=parseInt(l);R=parseInt(r);switch(o){case '*':return R*L;case '\/':return R/L;}};s=prompt();M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;while(M.test(s)){s=s.replace(M,m)};alert(eval(s))

This solution would mean that people can enter ANY JavaScript code they want! The 303 character version does not execute any JavaScript; it only does math.

JavaScript (303 characters compacted)

Since I'm ending lines with semi-colons, all removable whitespace was ignored for character counting, but left in for clarity.

JavaScript (303 characters compacted, 204 if I'm willing to sacrifice my soul)

Since I'm ending lines with semi-colons, all removable whitespace was ignored for character counting, but left in for clarity.

I can get it down to 204 characters if I use eval(), but that's not how I roll... (JS-Fiddle)

function m(x,l,o,r){L=parseInt(l);R=parseInt(r);switch(o){case '*':return R*L;case '\/':return R/L;}};s=prompt();M=/(\-?\d+)([\*\/])(\-?\d+)(?=[^\*\/]*$)/;while(M.test(s)){s=s.replace(M,m)};alert(eval(s))

This solution would mean that people can enter ANY JavaScript code they want! The 303 character version does not execute any JavaScript; it only does math.

Source Link
IQAndreas
  • 1.2k
  • 6
  • 11
Loading