Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

javascript - Get number from one to x in a loop?

How do I get all the digits from 0 - x if x is a variable like so:

var x = 5

Then loop then so it logs them all individually by console.log()?

I was thinking I could use .each(), but it might make more sense to use something like for or while. I would post code, but I honestly can't think of a good/possible way to do this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like this? http://jsfiddle.net/howderek/KKgPT/

x = 5;
for (i = 0;i <= x;i++) {
    console.log(i);
    document.getElementById("result").innerHTML += i + ' ';
}?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...