tags:

views:

65

answers:

3

can this array of numbers sequence be represented in less characters (excluding whitespace) than this?

i = [0,1,2,3,4,5,6,7,8,0,3,6,1,4,7,2,5,8,0,4,8,6,4,2];

(52 chars)

or a function i which returns the same values in under 52 chars

the aim is to reduce the number of characters used to represent the code.

+3  A: 

i='012345678036147258048642'.split('');

dev-null-dweller
thats good but returns a character array rather than an integer array
PeanutPower
If looping through this array, adding `parseInt()` will still be 49 characters ;)
dev-null-dweller
yeah but parseInt will be repeated multiple times, i have more than one reference to i
PeanutPower
is there any way to cast the character array to an integer array in concise code?
PeanutPower
@dev-null-dweller, what about using `+` instead of `parseInt()`? ;-)
Andy E
Good idea, but rather `*1`, because `+` will concatenate two chars into string. And still it only applies when looping through array to have it in one place for all elements.
dev-null-dweller
+1  A: 
scunliffe
+1  A: 

I would always go with the comma separated array,

it's less overhead than any conversion process.

But the idea of a quick method to turn a string of digits

into an array of numbers appealed to my evil twin...

String.prototype.dA= function digitArray(){
    return eval('['+this.replace(/(\d)/g,'+$1,')+']');
}

i='012345678036147258048642'.dA(); (34 characters)

// test i
for(var j= 0, L= i.length; j<L;j++){
    i[j]= i[j]+' ('+typeof i[j]+')';
}
i.join(', ')

/* returned value: 0 (number), 1 (number), 2 (number), 3 (number), 4 (number), 5 (number), 6 (number), 7 (number), 8 (number), 0 (number), 3 (number), 6 (number), 1 (number), 4 (number), 7 (number), 2 (number), 5 (number), 8 (number), 0 (number), 4 (number), 8 (number), 6 (number), 4 (number), 2 (number) */

kennebec
+1 for your evil twin :)
PeanutPower