2012-10-21 56 views
-3

即时试图在JavaScript的是满足该提示JavaScript的倒计时程序

写JavaScript函数 COUNTDOWN(ⅰ) ,它接受一个整数参数和从i到0返回\倒计时”写一个程序,用 每个号码。 例如之间出现空间,倒计时(5)应返回字符串“5 4 3 2 1 0”。至于RST问题,则可能 希望测试在计算机上的解决方案。

SOFAR我有这个

var i= ""; 

function countdown(i) 

{ 

    while(i > 0) 

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

能有人帮我im很新的编程

+2

java的!= JavaScript的... – assylias

+0

https://developer.mozilla.org/en-US/docs/JavaScript/A_re-introduction_to_JavaScript – clentfort

+0

我认为这是某种形式的家庭作业,你想一个评论和解释回答? –

回答

0

你想要在你的倒计时0,所以你要

while (i >= 0) 

,而不是while (i > 0),这在年底排除0


而且,提到的一些评论,var i = ""定义i是一个,所以你不能像i--执行操作。您应该将i定义为整数,例如var i = 5

0

下面是答案:

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

countdown(5); 
1

希望这使得足够的理智:

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

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); 

这里使用递归另一种解决方案(位更先进的),替代了/ while循环,其中一个函数调用自身:

// here is an implementation using recursion 
function countdown(i)  
{ 
    if(i<=0)  
     return i; 
    return i + " " + countdown(--i); 
} 
+0

有一次,你明白这一点,请参阅Fabricio的回答以及他如何重构这一逻辑。 – cbayram

+0

感谢提到,我倾向于使东西稍微比必要的更复杂。您的意见非常详细,详细级别为+1。 –

+0

由于某种原因,你们给我的代码不要在便笺簿中重复输入,他们只是输出我放入的第一个数字 – user1763859

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)); 
0

递归方式;)

​var countdown=function(i) { 
    console.log(i); 
    i>0 && countdown(i-1); 
} 
    countdown(10);