Home > other >  Why am I getting one empty array item in every other array like a pattern? (JavaScript)
Why am I getting one empty array item in every other array like a pattern? (JavaScript)

Time:07-14

UPDATE AT THE BOTTOM OF THE POST!


This is the first part of a program that will eventually make SVG star polygons.

Only star polygons that can be made in one go, I.e. like a Pentagram, NOT like a Hexagram that can only be made with at least 2 shapes I.e. 2 inverted and overlapping triangles.

I'm almost finished with this. Everything seems to be working well and I'm getting the out put I want except there's an empty array item in every other array produced for some reason.

In this part

I'm printing to the console (for testing) an object with items named with numbers that represent the number of points / sides of a regular polygon. Within each item is an array with the lower half of all the non factoring numbers of that item (number) I.e. an array of numbers that when the number (item name) is divided by them, it will return a fraction.

E.g. Object

  • starPolygons{'5': 2, '7': 3, 2, '8': 3, '9': 4, 2, …}
  • (Representation of: Pentagon: Heptagon: Octagon: Nonagon: …)

What "qty_test" functions does

The points variable (= 8 for example) is sent to a function (qty_test) which is used to find the number of divisors I need to test and inside it is divided by 2. If the result is a fraction, it will return the result rounded back, and if its even, it will return the result - 1.

//8 has more divisors to cycle through -so its a better example!
//In the real program, it will be 5.
let points = 8;
let starPolygons = {};

E.g. qty_test

  1. point (=8) ()=> n /= 2 (=4)
  2. if n - n(rounded-back) = 0?
  3. true returns (n -1) and false returns n(rounded-back)
function qty_test(n) {

    n /= 2;

    let divisorQTY = n - Math.floor(n) !== 0;


    return divisorQTY ? Math.floor(n) : (n - 1);
};

What divisor_test functions does

This function uses the number returned from the previous function (n) to set a for loop that tests every cycle if n is not a factor of the value of the points variable. I.e. points divided by n returns a fraction and if it returns true, meaning a fraction was produced n`s value is indexed in an array and decreaces by 1 (--n).

E.g. divisor_test

  • n(=8) / 2 = 4 << 2 will be ignored.
  • n(=8) / 3 = 2.666... << 3 will be collected
function divisor_test(n) {

    let nonFactors = [];

    n = qty_test(n);

    for (let index = 0; index <= n; index  ) {

        let quotient = (points / n) - Math.floor(points / n) !== 0;
        quotient ? nonFactors[index] = n-- : --n;

    };
    return nonFactors;
};

The program cycles through 5 - 360 and indexes an object with numbers (5-360) and array list of their lower half non factor numbers.

while (points <= 360) {

    nonFactors = divisor_test(points);

    let testArray = nonFactors.length;

    if (testArray) starPolygons[String(points)] = nonFactors;

    points  ;
};
console.log(starPolygons);

AS YOU CAN SEE IN THIS IMAGE. THE PATTERN OF EMPTY ARRAY INDEXES / ITEMS

enter image description here


UPDATE: I NOTICED THE PATTERN OF ARRAYS WITH THE EMPTY SLOTS HAS AN INTERESTING QUALITY TO IT. This is so cool and so confusing as to what causes this... enter image description here enter image description here

CodePudding user response:

The culprit is that in the line with checking the quotient

quotient ? nonFactors[index] = n-- : --n;

you added setting the value only for the case when it is present that is equal to true but you didn't add to the case when it is false correct solution is:

quotient ? nonFactors[index] = n-- : nonFactors[index] = --n;.

CodePudding user response:

During the Iteration you are forcing the array to add an empty item. So, the empty object appears when the array has only two items in it and you are trying to nonFactors[3] = n--; and thats what causes the issue.

So a more simplified version of the for loop with the solution will be

for (let index = 0; index <= n; index  ) {

    let quotient = (points / n) - Math.floor(points / n) !== 0;
    
    if(quotient){
        if(index > nonFactors.length){
            nonFactors[index - 1] = n--    
        }else{
            nonFactors[index] = n--    
        }
        
    }else{
        n = n -1;
        
    }
};
  • Related