最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I use for loop to count numbers? - Stack Overflow

programmeradmin1浏览0评论

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. I am very confused with using the parameters vs function name when you return. 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 need to count numbers upward and have it print out with a string "then" in between: 5 then 6 then 7 then... like this. I am very confused with using the parameters vs function name when you return. 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;
}
Share Improve this question edited Nov 26, 2015 at 5:38 Shanoor 13.7k2 gold badges31 silver badges41 bronze badges asked Nov 26, 2015 at 5:00 jso1226jso1226 591 gold badge2 silver badges8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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.

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. You'd probably want to start yours at 0 (var i = 0).

We can use JavaScript join function as well to achieve this Code

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 inplete (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

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

发布评论

评论列表(0)

  1. 暂无评论