-3

このプロンプトを満たすJavaScriptでプログラムを作成しようとしています

整数の引数を取り、i から 0 までの \countdown を返す JavaScript 関数 countDown(i) を記述します。各数値の間にはスペースが入ります。たとえば、countDown(5) は文字列「5 4 3 2 1 0」を返す必要があります。 "。最初の問題については、コンピューターでソリューションをテストすることをお勧めします。

私はこれを持っています

var i= "";

function countdown(i)

{

    while( i > 0)

    {
        console.log(integer);
        i--;
    }
}
countdown();

誰かがプログラミングに非常に慣れていない私を助けてくれませんか

4

6 に答える 6

1

Hopefully this makes enough sense:

function countdown(i) {
    //initialize the variable to be returned with the initial i
    var ret = i;
    //in each iteration, assigns i to i-1 then checks if i >= 0
    while (--i >= 0) {
        //concatenates a space and the current i value to the return string
        ret += ' ' + i;
    }
    //returns the string
    return ret;
}

Fiddle

于 2012-10-21T22:27:07.673 に答える
1

私がコードに入れたコメントを読んで学んでいただければ幸いです。

// you write comments in JavaScript with two forward slashes
// i is the integer parameter of your countdown function
// i is passed to countdown when called, i.e. countdown(9)
function countdown(i)    
{
    // this is an ret string variable that is private to the countdown function
    // you can't access ret from outside of this function
    var ret = "";   

    // your loop should include 0 according to your requirements
    while( i >= 0)
    {
        // here you are appending i to your ret string which you'll return at the end of this function      
        ret += i;// += is a short hand form of saying ret = ret + i

        // you want to append an empty space for every i except the last one (0)
        if(i > 0) {
            ret += " "; 
        }
        i--;    // here you are decrementing i
    }
    return ret;
}
// here you are making the actual call to the function with integer 5 
// you are assigning the returned value of your function call to result variable
var result = countdown(5);  

// here you are printing your result string variable to the log
console.log(result);

関数が自分自身を呼び出す for/while ループの代わりに、再帰を使用する別のソリューション (もう少し高度な) を次に示します。

// here is an implementation using recursion
function countdown(i)    
{
    if(i<=0)    
        return i;
    return i + " " + countdown(--i);
}
于 2012-10-21T22:27:23.967 に答える
0

0カウントダウンに含めたいので、

while (i >= 0)

とは対照的に、最後while (i > 0)に除外0します。


また、言及されたコメントのいくつかのように、はstringvar i = ""であると定義iされているため、 のような操作を実行することはできません。たとえば、整数になるように定義する必要があります。i--ivar i = 5

于 2012-10-21T22:13:42.763 に答える
0

再帰的な方法;)

​var countdown=function(i) {
    console.log(i);
    i>0 && countdown(i-1);
}
    countdown(10);
于 2012-10-21T22:47:54.883 に答える
0

Here is the answer:

 function countdown(i) {
   answer = '';
   while( i >= 0) {
      answer = answer + i.toString() + ' ';
      i--;
   }
   return answer;
 }

 countdown(5);
于 2012-10-21T22:25:38.823 に答える
0

再帰を使用してそれを行う方法は次のとおりです。

//define countdown function
var countdown = function countdown(i) {
    //loop while the passed in parameter "i" is >= 0
    while (i >= 0) {
        //return a space concatenated with the value of i
        //and call the countdown function again (by
        //concatenating the result) to continue counting down
        return ' ' + i + countdown(i -= 1);
    }
    return ''; //out of loop, return an empty string
};
console.log(countdown(5));
于 2012-10-21T22:27:43.673 に答える