我希望大家看完我已经把代码和学习的意见。
// 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);
这里使用递归另一种解决方案(位更先进的),替代了/ while循环,其中一个函数调用自身:
// here is an implementation using recursion
function countdown(i)
{
if(i<=0)
return i;
return i + " " + countdown(--i);
}
java的!= JavaScript的... – assylias
https://developer.mozilla.org/en-US/docs/JavaScript/A_re-introduction_to_JavaScript – clentfort
我认为这是某种形式的家庭作业,你想一个评论和解释回答? –