简体   繁体   English

如何使用for循环对数字进行计数?

[英]How can I use for loop to count numbers?

I need to count numbers upward and have it print out with a string "then" in between: 5 then 6 then 7 then... like this. 我需要向上计数数字,并用介于两者之间的字符串“ then”将其打印出来:5、6、7,然后……。 I am very confused with using the parameters vs function name when you return. 返回时,我对使用参数vs函数名称感到非常困惑。 My code is below.. but could someone help with this? 我的代码在下面..但是有人可以帮忙吗?

function countUp(start) {
  start +=    
  for(var i = start; i < start + 10; i++) {
    console.log(start[i] + "then");
  }
  return start;
}

I would do something like this: 我会做这样的事情:

function countSheep(limit){
    for (var i = 1; i < limit; i +=1){
        console.log(i + " sheep")
    }
}

countSheep(10);

I used "sheep" instead of "then", but you get the idea. 我使用“绵羊”代替“然后”,但是您明白了。 Since you just want to produce a side effect (print out a "1 then 2.." to the console, you don;t need to build up a string and then have your function return it. 由于您只想产生一个副作用(将“ 1然后2 ..”打印到控制台,因此您无需构建字符串,然后让函数返回它)。

If you did want to build up a string and then have your function return it though, you could do something like this instead: 如果您确实想建立一个字符串然后让函数返回它,则可以执行以下操作:

function countSheep(limit){

    var allMySheep = "";

    for (var i = 1; i < limit; i +=1){
        allMySheep += (i + " sheep, ") 
    }

    return allMySheep;
}

console.log(countSheep(10));

Note: I started my loops at 1 (var i = 1) because I'm counting sheep, not numbers. 注意:我从1(var i = 1)开始循环,因为我是在计算绵羊,而不是数字。 You'd probably want to start yours at 0 (var i = 0). 您可能想从0开始(变量i = 0)。

We can use JavaScript join function as well to achieve this Code 我们也可以使用JavaScript连接功能来实现此代码

function getCountStr(count) {
    var str =[];
    for (var i = 1; i <= count; i++) {
        str.push(i);
    }
   console.log(str.join(' then '));
}  

There are few issues with your code 您的代码很少有问题

function countUp(start) {
  start +=      // <<<<< what's this? It's an incomplete (and useless) statement
  for(var i = start; i < start + 10; i++) {
    console.log(start[i] + "then");
    //          ^^^^^^^^ why are doing this? you should only write i
  }
  return start; // you don't need to return anything
}

A cleaned and working version from your code 您的代码中已清理且可以使用的版本

function countUp(start) {
  for(var i = start; i < start + 10; i++) {
    console.log(i + " then ");
  }
}

But this code will have an extra 'then' at the end like 1 then 2 then , so here's a code that will handle this 但是此代码的末尾将有一个额外的“ then”,例如1 then 2 then ,因此这是将处理此问题的代码

 function countUp(start) { // a temporary array to store your numbers var tmpArr = []; for (var i = start; i < start + 10; i++) { // store the count into the array tmpArr.push(i); } // display the count by putting ' then ' between each number var stringToDisplay = tmpArr.join(' then '); console.log(stringToDisplay); document.write(stringToDisplay); } countUp(1); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM