user3229531
user3229531

Reputation: 15

Need a code to count from one to ten in javascript

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?

Upvotes: -1

Views: 24849

Answers (8)

Surafel Fikru
Surafel Fikru

Reputation: 31

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

Upvotes: 0

pelayotrives
pelayotrives

Reputation: 169

I suggest to everyone in this situation to take a look at https://developer.mozilla.org/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.

Upvotes: 1

Jacksonkr
Jacksonkr

Reputation: 32227

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 recommend using syntactically friendly code like @jterry used, you'll thank yourself later.

Upvotes: 0

JaseyJS
JaseyJS

Reputation: 61

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

Upvotes: 1

Bryan
Bryan

Reputation: 2078

You could use a simple while loop:

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

    }
    countToTen() // function call

Upvotes: 0

vborutenko
vborutenko

Reputation: 4443

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++;
}

Upvotes: 0

jterry
jterry

Reputation: 6269

You just need a simple for loop:

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

Per your comment, 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

Upvotes: 3

Patrick Gunderson
Patrick Gunderson

Reputation: 3281

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

For this question, the code would look like;

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

Upvotes: 0

Related Questions