// ( ) , , // , var monstersAscendingAStaircase = function(numberOfSteps) { var stairs = []; stepsUntrodden = []; var largestGait = Math.sqrt(numberOfSteps); // ; // , for (var i = 2; i <= largestGait; i++) { if (!stairs[i]) { for (var j = i * i; j <= numberOfSteps; j += i) { stairs[j] = "stomp"; } } // , – for (var i = 2; i <= numberOfSteps; i++) { if(!stairs[i]) { stepsUntrodden.push(i); } } // return stepsUntrodden; };
for (var j = i * i; j <= numberOfSteps; j += i) {
function downTheRabbitHole(growThisBig) { var theFullDeck = Array(growThisBig); var theHatter = Function('return this/4').call(2*2); var theMarchHare = Boolean('The frumious Bandersnatch!'); var theVerdict = 'the white rabbit'.split(/the march hare/).slice(theHatter); // … eval(theFullDeck.join('if (!theFullDeck[++theHatter]) {\ theMarchHare = 1;\ theVerdict.push(theHatter);\ ' + theFullDeck.join('theFullDeck[++theMarchHare * theHatter]=true;') + '}') ); return theVerdict; }
theFullDeck
theFullDeck
, since the solution represents a deck of cards, and as a result, only those that correspond to prime numbers will lie face down.theMarchHare
theMarchHare
set to true
true
. When the Boolean constructor is called as a function, its argument is converted to true
true
or false
false
. In this case, the non-empty string 'The frumious Bandersnatch!' converted to true
true
. (By the way, such an assignment is not very necessary here, because in line 10 of theMarchHare
theMarchHare
assigned new value).theVerdict
theVerdict
an empty array, and does it as figuratively as possible: var theVerdict = 'the white rabbit'.split(/the march hare/).slice(theHatter);
split
split
Is a regular expression not matching 'the white rabbit', so when calling split
split
we get an array that contains only 'the white rabbit'. Subsequent slice
operation slice
enters the copy of the array all the elements of the original array, starting with the specified index. Since in our singleton array there is no index 1 (this is the value of theHatter
theHatter
), no members from it are copied, and we get an empty array. function downTheRabbitHole(growThisBig) { var theFullDeck = Array(growThisBig); var theHatter = 1; var theMarchHare = true; var theVerdict = [];
// … eval(theFullDeck.join('if (!theFullDeck[++theHatter]) {\ theMarchHare = 1;\ theVerdict.push(theHatter);\ ' + theFullDeck.join('theFullDeck[++theMarchHare * theHatter]=true;') + '}') );
eval
function eval
let's talk about nested join
instructions join
. The join function turns an array into a string, while its argument serves as glue between the elements of the array. If you call join
join
applied to an empty array, we get a string consisting of solid glue (repeated n - 1 times, where n is the length of the array): Array(4).join('hi'); //'hihihi'
Array(4).join('A' + Array(4).join('a')); //'AaaaAaaaAaaa'
var arr = [], count = 0; Array(4).join('arr.push(' + Array(4).join('count++,') + '-1);'); //"arr.push(count++,count++,count++,-1);arr.push(count++,count++,count++,-1);arr.push(count++,count++,count++,-1)"
eval
eval
... var arr = [], count = 0; eval(Array(4).join('arr.push(' + Array(4).join('count++,') + '-1);')); arr; //[0, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8, -1] … . : // ... eval(theFullDeck.join('if (!theFullDeck[++theHatter]) {\ theMarchHare = 1;\ theVerdict.push(theHatter);\ ' + theFullDeck.join('theFullDeck[++theMarchHare * theHatter]=true;') + '}') ); eval ( ) : if (!theFullDeck[++theHatter]) { theMarchHare = 1; theVerdict.push(theHatter); theFullDeck[++theMarchHare * theHatter] = true; theFullDeck[++theMarchHare * theHatter] = true; theFullDeck[++theMarchHare * theHatter] = true; } if (!theFullDeck[++theHatter]) { theMarchHare = 1; theVerdict.push(theHatter); theFullDeck[++theMarchHare * theHatter] = true; theFullDeck[++theMarchHare * theHatter] = true; theFullDeck[++theMarchHare * theHatter] = true; } if (!theFullDeck[++theHatter]) { theMarchHare = 1; theVerdict.push(theHatter); theFullDeck[++theMarchHare * theHatter] = true; theFullDeck[++theMarchHare * theHatter] = true; theFullDeck[++theMarchHare * theHatter] = true; } // etc...
theFullDeck
- this is an array with all the numbers that need to be checked, theHatter
theHatter
and theMarchHare
theMarchHare
- nested counters, multiplied with each increment, to generate all possible composite numbers. All cards whose indices are compound numbers are reversed (because theFullDeck
theFullDeck
with this index is true
true
). Only those cards that correspond to prime numbers remain open. // , , JavaScript... function kevinTheNumberMentioner(_){ l=[] /* --> */ with(l) { // , ... for (ll=!+[]+!![];ll<_+(+!![]);ll++) { lll=+!![]; while(ll%++lll); // (ll==lll)&&push(ll); } forEach(alert); } // ... return [!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]]; }
for (ll=!+[]+!![];ll<_+(+!![]);ll++) {
for
loop for
and ll and _ are the variable names. All the rest is literal and shaped brain removal.!+[]+!![]
!+[]+!![]
. Having examined this expression, we see in it two empty array literals. Before the first one stands +, because of which the array is forcibly reduced to the number 0. Right in front of it, !, forcibly leading 0 to its Boolean opposite, that is, to true
true
. So !+[]
!+[]
results in true
true
.!!
!!
that will simply force him to the boolean. Since arrays are always objects, the boolean of an array is always true
true
. So, !![]
!![]
always gives true
true
.!+[]+!![]
!+[]+!![]
, in fact, we have true + true
true + true
. Here, + forces both operands to the number 1, so the final result of the whole expression is 2.for
loop for
now understandable without difficulty. Again, we have !![]
!![]
, this time before it goes +, forcibly leading true
true
to 1. So, ll<_+(+!![])
ll<_+(+!![])
gives ll < _ + 1
ll < _ + 1
.for
loop for
gives: // , , JavaScript… function kevinTheNumberMentioner(max){ var result = []; /* --> */ with(result) { // , ... for (candidate = 2; candidate < max + 1; candidate++) { var factor = 1; while (candidate % ++factor); // (candidate == factor) && push(candidate); } forEach(alert); } // ... return '42'; }
with
with
It refers to the most reprehensible from the point of view of "observers of the purity of JavaScript", but still in line 3 - it is she. Javascript will try to enclose all the properties not referenced in the with
block with
given object. Consequently, the restless methods of the push
array push
and forEach
forEach
will be in the scope of the result
result
.while
while
in the ninth row. This cycle has no body, therefore factor
factor
just continues to increase by one until it is fully divided, giving the candidate
candidate
as private. The next line checks if the values ​​of the candidate
are now equal. candidate
and factor
factor
. In this case, the number has no smaller dividers, therefore, it is simple and is added to the result
result
.alert
alert
. Finally, the program returns 42. function MrsPrimmerwicksProgeny(MaxwellNumberby) { Number.prototype.isAPrimmerwick = function() { for (var AddableChopper = 2; AddableChopper <= this; AddableChopper++) { var BittyRemnant = this % AddableChopper; if (BittyRemnant == 0 && this != AddableChopper) { return console.log( 'It is a composite. The dear, gentle, patient, noble', +this, 'is a composite'), false; } } return console.log( 'Oh', +this, +this, +this, 'what a happy day this is for you and me!'), true; } var VenerableHeap = []; for (var AveryNumberby = 2; AveryNumberby <= MaxwellNumberby; AveryNumberby++) { if (AveryNumberby.isAPrimmerwick()) { VenerableHeap.push(AveryNumberby); } } return VenerableHeap; }
6..isPrime(); // 7..isPrime(); //
Number.prototype
Number.prototype
. Its own extension is called isAPrimmerwick
isAPrimmerwick
(in fact, all objects here have fancy Dickens names) and is defined in lines 2-14. In lines 17-21, we simply ask each number whether it is simple and add simple numbers to the results array called VenerableHeap
VenerableHeap
.isAPrimmerwick
method isAPrimmerwick
isAPrimmerwick
basically simple. We divide the existing number into all possible dividers. If this or that number is divided without remainder, then it is composite, otherwise - simple.this
this
(but prefixed with + to force it from a numeric object to a primitive). Secondly, Dickens uses a comma operator to simultaneously call console.log
console.log
and return a boolean value. var yearOfTheLighteningQuickAtkinSieve = function(tops) { //BP #40 07-14 //ELEPHANT BUTTE, NM var NSRS/*[1]*/ = [0,0,2,3]; /* , i j ( 1) 1 (, ). */ for(var i = 1; i < Math.sqrt(tops); i++){ for(var j = 1; j < Math.sqrt(tops); j++){ if (i*i + j*j >= tops) { break; } /* (.e. i j) quadratic, (n). */ var n = 4*i*i + j*j; /* (.e. n) 12, 1 5, (.e. n) [2]. */ if(n <= tops && (n%12 == 1 || n%12 == 5)){ NSRS[n] = NSRS[n] ? 0 : n; } /* (.e. JavaScript) , and again the result () n. */ n = 3*i*i + j*j; /* (.e. n) 12, 7 , (.e. n) */ if(n <= tops && (n % 12 == 7)){ NSRS[n] = NSRS[n] ? 0 : n; } /* (.e. ), , , (.e. JavaScript) . , ( ) ( ) n. */ n = 3*i*i - j*j; /* ( ) , , (i) .e. ( ) (j) [3]. */ if (i>j) { if((n <= tops) && (n % 12 == 11)){ NSRS[n] = NSRS[n] ? 0 : n; } } } } /* ( ) (.e. JavaScript) , : (. ) (.. ) */ for(i = 5; i < Math.sqrt(tops); i++){ if(NSRS[i] == 1){ for(j = i*i; j < tops; j += i*i){ NSRS[j] = 0; } } } return NSRS.filter(Number); // [4] } /* [1] . [2] , [a] 0, 0 . [3] [a] . [4] `Array.prototype.filter` , EcmaScript-262 (5- ) [b]. `Number` - , . Array.prototype.filter (.e. ) , 0, (.e. ) , `Array.prototype.filter`. [a] .e. , true. [b] http://es5.imtqy.com/#x15.4.4.20 */
return NSRS.filter(Number); // [4]
NSRS
NSRS
. Here it is a sparse array containing all prime numbers, which, however, are interspersed with undefined values ​​(in front it is filled with zeros): [0, 0, 2, 3, undefined, 5, undefined, 7/*, etc.. */]
Array.prototype.filter
function Array.prototype.filter
creates a new array that contains only those elements of the original array for which the given function returns true
true
. This is about Number
Number
built-in language function that tries to force your argument to a number. Number
forcibly leads undefined
undefined
to NaN
NaN
, leaving all real numbers intact. Since both mean: NaN
NaN
and 0 means "false", the new array will contain only prime numbers: [0, 0, 2, 3, undefined, 5, undefined, 7].filter(Number); //[2, 3, 5, 7]
Source: https://habr.com/ru/post/275245/
All Articles