"JS" Converts the odd number of 1-100 to an array __js

Source: Internet
Author: User
Tags array length

The array is very important in the JS application, many places have used the array to store the data. Today summarize a small problem, but it is easy to forget the little knowledge points.

Requirement: Store the odd number between 1-100 in the array.

Analysis:

Suppose an array of arrays, looping to determine the initial value of Var i=1.

The first way to think about it should be that we judge the I, then put the qualifying I value in the array array[i], and finally print to see if the results are correct, the code is as follows:

var array=[];
for (var i = 1; i< i++) {
        if (i% 2!== 0) {
               Array[i] = i
        }
}
Console.log (array);

Printing results are:

[1:1, 3:3, 5:5, 7:7, 9:9, 11:11, 13:13, 15:15, 17:17, 19:19, 21:21, 23:23, 25:25, 27:27, 29:29, 31:31, 33: 33, 35:35, 37:37, 39:39, 41:41, 43:43, 45:45, 47:47, 49:49, 51:51, 53:53, 55:55, 57:57, 59:59, 61:61, 63:6 3, 65:65, 67:67, 69:69, 71:71, 73:73, 75:75, 77:77, 79:79, 81:81, 83:83, 85:85, 87:87, 89:89, 91:91, 93:93, 95:95, 97:97, 99:99]
We can see from the result that the subscript of the array is not contiguous, but the array we think of is supposed to be contiguous, so why does this problem occur? Because when we make judgments in the for loop, they are eligible to be stored in an array, at which point I is discontinuous.

Solution:

I've given three simple examples here, and there may be other ways ^ ^.

method One:

var array=[];
for (var i = 1; i< i++) {
     if (i% 2!== 0) {
        array.push (i);
       }
}
Console.log (array);

The push () method adds one or more elements to the end of the array and returns a new length. Method Two:

var array = [];
var j = 0;
for (var i = 1; i< i++) {
    if (i% 2!== 0) {
         array[j] = i;
         j + +;
    }
}
Console.log (array);
Method Three:

var array = [];
for (var i = 1; i< i++) {
      if (i% 2!== 0) {
           Array[array.length] = i
        }
}
Console.log (array);
This method is more ingenious, I would like to explain, just start the length of the array is 0, then deposit 1, then the array length plus 1, and then into 3, in order to push, the final result is what we want.
Print results:

[1, 3, 5 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55 , 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.