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

I Want To Print 1 to 100 Numbers Using Arrays In Javascript Only - Stack Overflow

programmeradmin4浏览0评论
<!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. 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. I'm Facing some errors in the above code. Please correct my mistakes to get the output..Thankyou in advance.

<!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. 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. I'm Facing some errors in the above code. Please correct my mistakes to get the output..Thankyou in advance.

Share Improve this question asked Nov 5, 2015 at 12:49 Manoj KarthikManoj Karthik 712 gold badges2 silver badges6 bronze badges 8
  • 2 points is an empty array/collection. so accessing it will give undefined values. change console.log(points[i]); to console.log(i); – rajuGT Commented Nov 5, 2015 at 12:51
  • 1 It worked! Thanks alot sir! – Manoj Karthik Commented Nov 5, 2015 at 12:52
  • 2 If you change it to console.log(i), you are no longer using arrays. You might as well change label to 100, and remove points altogether. This is not a very well phrased question as it does not indicate HOW you are to use arrays. – ergonaut Commented Nov 5, 2015 at 12:56
  • 1 If you change it to console.log(i), you are no longer using arrays - you'll also get 0 to 99 instead of the required 1 to 100 – Jaromanda X Commented Nov 5, 2015 at 12:58
  • 1 @ManojKarthik try to upvote questions that helped you. You can only "accept" one question, but you should still upvote other questions that helped you. That way if someone else stumbles across this thread in the future, it's easy to figure out which answers helped the most people. Welcome - and best of luck on this forum! – Stan Shaw Commented Nov 5, 2015 at 13:10
 |  Show 3 more comments

8 Answers 8

Reset to default 6

This will print 1-100 without any loops

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

he said he wants to print 1-100 from an ARRAY...So the array needs to be populated, first. 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.

First initialize the array.

var oneToHundredArray = [];

Now populate it with values 1 to 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 :)

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

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.

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.

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

output console.log logs numbers 1-100 respectively.

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"

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

Using a for loop:

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

get_array()
发布评论

评论列表(0)

  1. 暂无评论