-3

我正在尝试用 JavaScript 编写一个满足此提示的程序

编写一个 JavaScript 函数 countDown(i),它接受一个整数参数并返回一个从 i 到 0 的 \countdown",每个数字之间有一个空格。例如,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


此外,正如提到的一些评论,var i = ""定义istring,所以你不能执行像i--. 您应该定义i为整数,例如var i = 5.

于 2012-10-21T22:13:42.763 回答
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

递归方式;)

​var countdown=function(i) {
    console.log(i);
    i>0 && countdown(i-1);
}
    countdown(10);
于 2012-10-21T22:47:54.883 回答
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 回答