循环获取从1到x的数字? [英] Get number from one to x in a loop?

查看:70
本文介绍了循环获取从1到x的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果x是像这样的变量,如何从0-x中获取所有数字:

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

var x = 5

然后循环,以便通过console.log()分别记录所有日志?

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

我当时以为我可以使用.each(),但是使用诸如for之类的代码可能更有意义.我会发布代码,但老实说,我想不出一种好的/可能的方法来实现此目的.

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.

推荐答案

这真的很基本:

 for (var i = 0; i < x; ++i) console.log(i);

(如果要在输出中包含x的值,请使用<=.)

(Use <= if you want to include the value of x in the output.)

这篇关于循环获取从1到x的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆