简体   繁体   English

我只想在 Javascript 中使用 Arrays 打印 1 到 100 个数字

[英]I Want To Print 1 to 100 Numbers Using Arrays In Javascript Only

<!DOCTYPE html>
<html>
<head>
    <title>100-Numbers</title>
</head>
<body>
    <script>
        var points = new Array(100);
        var label = points.length;
        for (var i = 0; i < label; i++) {
            console.log(points[i]);
        }
    </script>
</body>
</html>

This is my First question in Stackoverflow.这是我在 Stackoverflow 中的第一个问题。 As i am an beginner, Please bare me and i need alot of support from you people.由于我是初学者,请让我裸露,我需要你们的大量支持。 I m trying to print 1 to 100 numbers using arrays in javascript only.我正在尝试仅在 javascript 中使用 arrays 打印 1 到 100 个数字。 I'm Facing some errors in the above code.我在上面的代码中遇到了一些错误。 Please correct my mistakes to get the output..Thankyou in advance.请纠正我的错误以获得 output..提前谢谢。

he said he wants to print 1-100 from an ARRAY...So the array needs to be populated, first.他说他想从数组中打印 1-100 ......所以首先需要填充数组。 THEN, you can loop through the array.然后,您可以遍历数组。

        var points = new Array(100);
        for (var i = 0; i < 100; i++) {
            points[i] = i + 1; //This populates the array.  +1 is necessary because arrays are 0 index based and you want to store 1-100 in it, NOT 0-99.
        }

        for (var i = 0; i < points.length; i++) {
            console.log(points[i]); //This prints the values that you stored in the array
        }

The array values are uninitialized.数组值未初始化。 I'm assuming that you want to print the values 1 to 100 using arrays where the values 1 to 100 are inside the array.我假设您想使用值 1 到 100 在数组内的数组打印值 1 到 100。

First initialize the array.首先初始化数组。

var oneToHundredArray = [];

Now populate it with values 1 to 100.现在用 1 到 100 的值填充它。

for(var value = 1; value <= 100; value++) {
    oneToHundredArray.push(value);
}

Now the contains the values you want.现在 包含您想要的值。 Just loop and print over it now.现在只需循环并打印即可。

for(var index = 0; index < oneToHundredArray.length; index++) {
    console.log(oneToHundredArray[index]);
}

Done :)完毕 :)

这将打印 1-100 没有任何循环

 Array.from({length: 100},(_,x) => console.log(x+1))
Array.from(Array(100), (_,i) => console.log(i+1));

The second parameter acts as mapping callback, so you also do this...第二个参数作为映射回调,所以你也这样做......

 const arr = Array.from(Array(100), (_,i) => i+1);
 for(num of arr) {
     console.log(num);
 }

Reference: Array.from参考: Array.from

You should start off with an empty array, then run a loop for 1-101, I logged the iterator so you can see the values populate, you then need a binding agent to hold the value of the iteration, then you would need to push those values to your empty array.您应该从一个空数组开始,然后为 1-101 运行一个循环,我记录了迭代器以便您可以看到填充的值,然后您需要一个绑定代理来保存迭代的值,然后您需要推送这些值到你的空数组。

var numbersArray = [];
   for( var i = 1; i <101; i++){
       console.log(i);
       var numbers = i;
       numbersArray.push(numbers);
   }

After that, you then need to run a loop for the length of the numbersArray to output the individual results.之后,您需要针对 numbersArray 的长度运行循环以输出单个结果。

for(var m=0; m<= numbersArray.length -1; m++){
       console.log(numbersArray[m]);
   }

output console.log logs numbers 1-100 respectively.输出 console.log 分别记录数字 1-100。

 var label = new Array(100); for (var i = 0; i < 100; i++) { label[i] = i + 1; } for (var i = 0; i < label.length; i++) { console.log(label[i]); }

It's much more easier with "while"使用“while”更容易

var i = 1;
while (i < 100) {
  document.write(i + "<br/>");
  i++;
}

Using a for loop:使用for循环:

function get_array() {
     var arr = [];
     for(var i=1; i<=100; i++) {
           arr.push(i);
     }
     console.log(arr); 
} 

get_array()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在javascript中打印偶数个子数组 - how do i print even numbers of sub arrays in javascript 使用JavaScript在客户端验证仅接受文本框中的1到100之间的数字 - Validate at client to accept only numbers between 1 to 100 in a textbox using JavaScript 我想使用 JavaScript 格式化数字 - I want to format numbers using JavaScript 我正在创建一个数组以响应打印图像 select 仅基于数字我想要的图像 - i am create an array in react to print images select only images i want based on to numbers 我想使用 hash 字符 javascript 打印一个正方形 - I want to print a square using the hash characters, javascript 仅打印负数及其总和 aplaying 循环和数组 - Print only negative numbers and its sum aplaying loops and arrays 我要打印页面时javascript中断 - javascript breaks when I want to print a page 如何让我的程序打印 100 个随机数并以正确的方式将它们从 1 到 100 排序 - How do I get my program to print 100 random numbers and sort them from 1 to 100 in a correct way 想要使用JavaScript打印页面的特定部分 - Want to print a particular section of a page using JavaScript 我有一个打印数字的 for 循环。 但我不想它打印几个数字 - I have a for loop that print's numbers. But I don't want it to print a few numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM