I'm 3 months into Javascript, and find it has no respect for its own declarations.
I have declared an empty array, and want to push a number to it, so that it returns an array of numbers (i.e., separable). However, I am told .push() is not a function. Concat() will not work either, and of course += ruins the algorithm.
How do I get it to accept each next value into an array? I have tried using 'new Array()', but it does not assist.
Similar to, but not the same as, Array.push throwing TypeError for array.push is not a function
In brief:
const fn = (n) => {
let factors = [];
let index = 1;
while (n % 2 == 0){
let out = 2 ** index;
factors.push(out);
index++;
n /= 2;
}
}
This returns:
Uncaught TypeError: factors.push is not a function
(I have left out a lot of code that does not affect the issue.)
Edit update: WIP. Apparently, the loop this is enclosed in has an effect, as do other variable declarations.
With any luck, I will return with a different question, having solved the initial problem.