我希望您阅读我在代码中的注释并学习。
// 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);
}