ループの理解
私はJsheroで問題を解決していました.ネットと私はそれを混乱させるので、明確な理解を得るために自分自身への解決策を説明しようとしました.
私がそれを正しく理解したならば、質問、答えと簡単な説明を研究して、私に知らせてください.
安全に!
質問
自然数Nを取る関数空間を書き込み、n個のスペースの文字列を返します.
例:スペース( 1 )は''を返さなければなりません.
回答
関数空間( num ) {
みましょう
一方( num -- 0 )
mySpaces += ' ';
Myspaceを返す//
解説
declare a function spaces
it has 1 parameter 'num'
declare a variable mySpaces
initialize it with an empty string
we use a while loop as we need to repeat an action multiple times
in this case we need to add blank spaces to a string
our '''empty spaces string''' will be stored in the variable mySpaces
blank spaces will be equal to the 'num' parameter, which is fed when the function 'spaces' is called
while loops have: a condition and a code (loop body)
our condition is to ensure that the code should execute as long as the num is greater than 0
our condition is (num-- > 0)
our code is: mySpaces += ''
so if our function is called with the following parameter then how would this while loop workspaces (3)
first it will check if 3 > 0; which it is and it will execute the code
mySpaces = mySpaces + ' '; the result in variable mySpace will now be ' ' (1 space)
then since we have used the num as a counter (num--), the loop will reduce 1 from 3 = 2
it will check if 2 > 0; which it is and it will execute the code
mySpaces = mySpaces + ' '; the result in variable mySpace will now be ' ' (2 spaces)
then since we have used the num as a counter (num--), the loop will reduce 1 from 2 = 1
it will check if 1 > 0; which it is and it will execute the code
mySpaces = mySpaces + ' '; the result in variable mySpace will now be ' ' (3 spaces)
then since we have used the num as a counter (num--), the loop will reduce 1 from 1 = 0
it will check if 0 > 0; which it is not and it will only execute the code after the while loop
we return the mySpaces variable which will give us the final resultReference
この問題について(ループの理解), 我々は、より多くの情報をここで見つけました https://dev.to/nparekh1979/understanding-while-loop-3gdh
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol