1

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.

2
  • 2
    If it’s just numbers between 0 and 9: "012345678036147258048642". For other bases you could use a different encoding. Commented Feb 12, 2010 at 17:45
  • If you can explain how this bit works, in the bigger picture, we might be able to provide a better option. Commented Feb 12, 2010 at 18:08

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

thats good but returns a character array rather than an integer array
If looping through this array, adding parseInt() will still be 49 characters ;)
yeah but parseInt will be repeated multiple times, i have more than one reference to i
is there any way to cast the character array to an integer array in concise code?
@dev-null-dweller, what about using + instead of parseInt()? ;-)
|
1

I don't necessarily recommend this... (because I don't think you should "wreck" the String object) but in theory you could do this:

<script>
  String.prototype.toNumericalArray = function(){
    var arr = this.split('');
    for(var i=0;i<arr.length;i++){
      arr[i] = 0+i;
    }
    return arr;
  }
</script>

Then when you want it, all you need is:

<script>
  s='012345678036147258048642';
  n = s.toNumericalArray();
  alert(typeof(n[3]));//number
  alert(typeof(n[7]));//number
  alert(typeof(n[9]));//number
</script>

And if you are really worried about the number of characters, you can minify all the variables / method name

i='012345678036147258048642'.x();

(thus if you exclude the initial prototype, any future calls would be 33 characters (for the same size list))

Comments

1

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) */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.