JavaScript (updated to work with all test cases)
The little-known truth is that there are actually four 6
s, but one of the betrayed the others and polymorphed into code form to eradicate them from the world digits of the numbers. Here is that traitorous six:
x=prompt(''+
'Enter number');
alert( ( (~x[
'ind'+
'exOf']('666')))?(x
.replace(/666(.*)$/,
function (mat,g){
return '667'+g
['re'+ 'place'
](/./g,0)})):((+x+
1+'').replace(
666,667)));
Here is an explanation. First, beautify the code and remove useless stuff like ''+'string'
and ((code))
:
x = prompt('Enter number');
alert(
~x['indexOf']('666')
?
x.replace(/666(.*)$/, function(mat,g) {
return '667' + g['replace'](/./g,0)
})
:
(+x+1+'').replace(666, 667)
);
Convert weird notations (like ~indexOf
and ['replace']
) into more common ones:
x = prompt('Enter number');
alert(
x.indexOf('666') > -1
?
x.replace(/666(.*)$/, function(mat, g) {
return '667' + g.replace(/./g, 0)
})
:
((parseInt(x) + 1) + '').replace(666, 667)
);
And now simply understand that the algorithm goes like this:
Old version (doesn't work for 666666666
):
s='Enter number';x
=prompt( ''+
s);x=+x+
(-~![]);
x=(''+x).replace('666',
666+([][ +[]]+[])
[+[]]['l ength'[
'repla'+ 'ce'](
/ /g,'')]);alert(x)
To understand this, let's first beautify it:
s = 'Enter number';
x = prompt('' + s);
x = +x + (-~![]);
x = ('' + x).replace('666',666+([][+[]]+[])[+[]]['l ength'['repla'+'ce'](/ /g,'')]);
alert(x);
Now let's remove useless things like '' + string
and 'str' + 'ing'
, remove the unnecessary s
variable, and change weirdness like -~![]
into 1
:
x = prompt('Enter number');
x = +x + 1;
x = ('' + x).replace('666', 666+"undefined"[0]['l ength'['replace'](/ /g,'')]);
alert(x);
'l ength'['replace'](/ /g,'')
is simply "length"
:
x = prompt('Enter number');
x = +x + 1;
x = ('' + x).replace('666', 666+"undefined"[0].length);
alert(x);
And "undefined"[0]
is "u"
, and "u".length
is 1
:
x = prompt('Enter number');
x = +x + 1;
x = ('' + x).replace('666', 666 + 1);
alert(x);
Now we're done! It should be pretty easy to understand now.
66700
. \$\endgroup\$