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

Need a code to count from one to ten in javascript - Stack Overflow

programmeradmin0浏览0评论

Need a simple code that can count from 1 -10 in javascript

I have tried :

function countToTen() {
    console.log("1");
    console.log("2");
    console.log("3");
    console.log("4");
    console.log("5");
    console.log("6");
    console.log("7");
    console.log("8");
    console.log("9");
    console.log ("10");
}

return countToTen;

but I guess this is wrong. Can anyone help please?

Need a simple code that can count from 1 -10 in javascript

I have tried :

function countToTen() {
    console.log("1");
    console.log("2");
    console.log("3");
    console.log("4");
    console.log("5");
    console.log("6");
    console.log("7");
    console.log("8");
    console.log("9");
    console.log ("10");
}

return countToTen;

but I guess this is wrong. Can anyone help please?

Share Improve this question edited Jan 23, 2014 at 20:43 Marco A. 43.7k27 gold badges143 silver badges260 bronze badges asked Jan 23, 2014 at 20:39 user3229531user3229531 151 gold badge1 silver badge1 bronze badge 1
  • 4 SEarch for a for loop. – Brad Commented Jan 23, 2014 at 20:40
Add a ment  | 

8 Answers 8

Reset to default 3

You just need a simple for loop:

for(var i = 1; i <= 10; i++) {
    console.log(i);
}

Per your ment, you can wrap this in a function as well:

function countToTen() {
    for(var i = 1; i <= 10; i++) {
        console.log(i);
    }
}

countToTen(); // Will write 1-10 to your console

The simplest function that uses for loops to count from 1-10 would look like as below.

function count() {
  for (number = 1; number <= 10; number++) {
    console.log(number);
  }
}
count();

I suggest to everyone in this situation to take a look at https://developer.mozilla/es/docs/Web/JavaScript/Reference/Statements/for to realize the possibilities of a 'For Loop'.

You can try 2 different methods. Remember that using let instead of var can be useful for you:

1. While

let i = 10; 
while (i <= 10) {
  console.log(i);
  i++;
}

2. For

for (let i = 0; i <= 10; i++) {
  console.log(i);
}

3. Extra: you can try to wrap your code in a function and reuse it later on.

function countNumbers() {
  for (let i = 0; i <= 10; i++) {
    console.log(i);
  }
}

(Remember to invoke it afterward, like:)

countNumbers();

If you want to start counting from 1, just assign 1 instead of 0 to 'i'.

Hope this helps. I know I'm late but I just wanted to make sure that this could be helpful to others as well.

The for loop is a fundamental ponent of javascript. You might want to check out http://jstherightway/ for a good primer.

For this question, the code would look like;

for (var i = 1; i <= 10; i++){
    console.log(i);
}

You can do this using loops

1. For

for (var i = 1; i <= 10; i++){
console.log(i);
}

2. While

var i = 1;
while(i <= 10){    
   console.log(i);
   i++;
}

You could use a simple while loop:

    function countToTen(){
        var i = 1;
        while(i <=10){
            console.log(i);
            i++;
        }

    }
    countToTen() // function call

let i=0;while(i++ < 10) console.log(i)

I swear there's a shorter way to do this using something along the lines of

Array(10).map(...) but maybe I'm low on coffee this morning.

THAT SAID I strongly remend using syntactically friendly code like @jterry used, you'll thank yourself later.

JavaScript is an imperative language and the answers here are pretty much great for JavaScript. Just for fun here is a functional way to do it. Its the perfect one liner.

[...Array(10).keys()].map((n) => console.log(n))
发布评论

评论列表(0)

  1. 暂无评论