Array filled with undefined elements

``````function permAlone(str) {
var num = 0;
function factorialize(num) {
var sum = 1;
for (i=1;i<=num;i++){
sum = sum*i;
}
return sum;
}
str = str.split("");
var options = [];
var combos = [];
for(i=0;i<str.length;i++){
options[i] = str.slice();
//console.log(str[i]);//????
for(j=0;j<(factorialize(str.length-1));j++){
combos[j] = str[i];
}
}
console.log(combos);
function choose(acc,curr,i){

}
return num;
}
``````

When I run this code combos becomes an array containing all undefined items. It has the correct length. When I `console.log(str[i])` I get only the first element of str. When I `console.log(str)` I get the whole str once, same when I `console.log(str[#])` with any number. Iâ€™m confused as to why this happensâ€¦ What am I missing?

which problem are you trying to solve?
if this, then i think you are over thinking it.

spoiler code below

``````function factorialize(num) {
var a = 1;
for (i = 1; i <= num; i++)
{
a = a * i;
}
num = a;
return num;
}
factorialize(20);
``````

Iâ€™m trying to solve â€śNo repeats pleaseâ€ť. I just used my code from â€śFactorialize a numberâ€ť to get the factorial of str.length-1

sorry i havenâ€™t made it to that one yet.
but looking at it now.

but iâ€™ve been playing with it in VS Code and i admit you had me stumped for a good 15 minutes.

then i realized you had more than one for loop using i as the iterator.

changing the problem area, to a gives what i think you are seeking.

``````    for (**a** = 0; **a** < str.length; **a**++)
{
console.log("str.length = "+str.length);
options[**a**] = str.slice();
console.log(**a**);
console.log(str[**a**]);

for (j = 0; j < (factorialize(str.length - 1)); j++)
{
combos[j] = str[**a**];
}
}
``````
1 Like