First scene , Ye Xiaofan won very beautiful . This inevitably met the envy of some of the big disciples outside .
“ Hum , But only won the first game , What makes you look good .”
“ namely , Yeah . Anyway? , Operators, after all, belong to the basic Kung Fu of comparison . Look at me , This ye Xiaofan is just lucky , I'm just familiar with this one .”
The first scene is over ,16 enter 8, Ye Xiaofan successfully promoted . The topic of the second scene , It's a calculation problem . use JavaScript calculation 1+2+…+100 Value of . Just across the street, while you're still busy calculating , Ye Xiaofan has completed the code .
var sum = 0; for(var i = 1;i < 101;i++){ sum = sum + i; } console.log(sum);
In the shocked eyes of his opponent , Ye Xiaofan began to explain calmly .
“ calculation 1+2+…+100 Value of , You can't add one by one directly , It's too slow , Low efficiency . So I thought of using a loop .for Loop is a good choice .for Circular format , There are three expressions in parentheses , When necessary for When cycling , Just execute expression one first . that is var
I = 1. Then execute the expression 2,I <
101. expression 2 Is a judgment condition , and if Sentence judgment has similarities and differences . When expression 2 The result of is Boolean true Time , It is considered to meet the conditions for entering the cycle , So I went back to execute {} Content in .”
sum = sum + i;
“ stay {} inside , Is a cumulative operation , Put each cycle i Add to variable sum up . When the code is executed , To execute the expression 3, that is i++, The meaning of this sentence is to let i The variable increases by one unit , So let i It's getting bigger , Until the conditions for entering the cycle are not met .”
Hear here , Lin Yuanqing nodded slightly . Ye Xiaofan continued ,“ I think the key point of this question is the understanding of circular technology . actually , Such a topic can also be used while Loop to do .” Say , Ye Xiaofan retyped a piece of code :
var sum = 0; var i = 0; while(i < 101){ sum += i; i++; } console.log(sum);
“while Cycle and for Differences in cycles , lie in while The loop has only one judgment expression , Like just now for Expressions in loops 2. as for for Circular expression 3, Already put {} It's gone . An expression is put into while Before the cycle . It's kind of like this .” after one's words , Ye Xiaofan wrote again , There is no delay in the process , Completely flowing , It seems that you are familiar with it .
var sum = 0; var i = 0; for(;i < 101;){ sum += i; i++; } console.log(sum);
“ Um , Well done , This one , Naturally, ye Xiaofan won .” Lin Yuanqing announces the results of the competition .
“ what ? Won again , It's too easy .” Ye Xiaofan was surprised , No wonder , Ye Xiaofan is usually under the guidance of Ye Lao , This difficult problem is really a little pediatrics .
Ye Xiaofan's second consecutive victory , Eight into four !
Technology