Skip to main content
added 1 characters in body
Source Link
gen_Eric
  • 730
  • 2
  • 10
  • 24

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+MathMath.pow(10, length).toString().substring(1),
        // temp loop variables
        index, value;
        
    for(index=index = 0; index < total; index++){
        // Convert number into its new base
        value = ''+indexindex.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split('').map(function(a){
            // Convert result to ints
            return +a;
        }));
    }
    
    return cards;
}

It's called using createCards(4,3).

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+Math.pow(10, length)).substring(1),
        // temp loop variables
        index, value;
        
    for(index= 0; index < total; index++){
        // Convert number into its new base
        value = ''+index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split('').map(function(a){
            // Convert result to ints
            return +a;
        }));
    }
    
    return cards;
}

It's called using createCards(4,3).

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = Math.pow(10, length).toString().substring(1),
        // temp loop variables
        index, value;
        
    for(index = 0; index < total; index++){
        // Convert number into its new base
        value = index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split('').map(function(a){
            // Convert result to ints
            return +a;
        }));
    }
    
    return cards;
}

It's called using createCards(4,3).

deleted 12 characters in body
Source Link
gen_Eric
  • 730
  • 2
  • 10
  • 24

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+Math.pow(10, length)).substring(1),
        // temp loop variables
        index, value;
        
    for(index= 0; index < total; index++){
        // Convert number into its new base
        value = ''+index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split('').map(function(a){
            // Convert result to ints
            return +a;
        }));
    }
    
    return cards;
}

It's called using createCards(4,3).

NOTE: The result is an array of strings, not ints, but that can easily be fixed.

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+Math.pow(10, length)).substring(1),
        // temp loop variables
        index, value;
        
    for(index= 0; index < total; index++){
        // Convert number into its new base
        value = ''+index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split(''));
    }
    
    return cards;
}

It's called using createCards(4,3).

NOTE: The result is an array of strings, not ints, but that can easily be fixed.

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+Math.pow(10, length)).substring(1),
        // temp loop variables
        index, value;
        
    for(index= 0; index < total; index++){
        // Convert number into its new base
        value = ''+index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split('').map(function(a){
            // Convert result to ints
            return +a;
        }));
    }
    
    return cards;
}

It's called using createCards(4,3).

deleted 12 characters in body
Source Link
gen_Eric
  • 730
  • 2
  • 10
  • 24

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+Math.pow(10, length)).substring(1),
        // temp loop variables
        index, value;
        
    for(index= 0; index < total; index++){
        // Convert number into its new base
        value = ''+index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split(''));
    }
    
    return cards;
}

It's called using createCards(4,3).

NOTE: The only issue here is the result is an array of strings, not ints, but that can easily be fixed.NOTE: The result is an array of strings, not ints, but that can easily be fixed.

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+Math.pow(10, length)).substring(1),
        // temp loop variables
        index, value;
        
    for(index= 0; index < total; index++){
        // Convert number into its new base
        value = ''+index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split(''));
    }
    
    return cards;
}

It's called using createCards(4,3).

NOTE: The only issue here is the result is an array of strings, not ints, but that can easily be fixed.

I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.

If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!

So, using that, I whipped up a function to create all numbers in that range as an array.

function createCards(length, max){
    var cards = [],
        // How many array elements
        total = Math.pow(max, length),
        // This will help with left padding
        // since JavaScript doesn't have str_repeat('0', length)
        pad = (''+Math.pow(10, length)).substring(1),
        // temp loop variables
        index, value;
        
    for(index= 0; index < total; index++){
        // Convert number into its new base
        value = ''+index.toString(max);
        // Left pad it so each string is the same length
        value = pad.substring(0, pad.length - value.length) + value;
        // Push into the array
        cards.push(value.split(''));
    }
    
    return cards;
}

It's called using createCards(4,3).

NOTE: The result is an array of strings, not ints, but that can easily be fixed.

Source Link
gen_Eric
  • 730
  • 2
  • 10
  • 24
Loading