0

I have a variable with a dynamic value (from 1 to 15). Depending on the value of the variable, I want to create an array with the same length.

And, if for example the variable value is 1, I want to store A in the array, if the value is 6 I want to store A,B,C,D,E,F in the array and so on.

How do I achieve this? Thanks in advance.

Brock Adams
94.1k23 gold badges243 silver badges313 bronze badges
asked Jul 30, 2010 at 7:51
7
  • 3
    Is this homework? If so, please tag it as such. Commented Jul 30, 2010 at 7:55
  • @Oded, Homework, in the middle of the summer? Commented Jul 30, 2010 at 7:55
  • 2
    @Anders - It is not summer in the south of our little globe, and not everyone in the world has summer vacation right now. Global community and all, you know? Commented Jul 30, 2010 at 7:58
  • Well, I am in Australia and it's winter here. No, it's not homework, thanks. Commented Jul 30, 2010 at 7:58
  • 2
    wait, people outside the US have internet? woah! Commented Jul 30, 2010 at 8:01

4 Answers 4

4
var sizeOfArray = 6;
"ABCDEFGHIJKLMNO".slice(0, sizeOfArray).split('');
// ["A", "B", "C", "D", "E", "F"]
answered Jul 30, 2010 at 7:59
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Anurag, so where do I create the array?
The second statement returns an array. The split function is doing that for you.
Well, he only mentioned an idea, not the full solution (nice idea btw). The full solution would be: var sizeOfArray=6; var newArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".slice(0, sizeOfArray).split('');
so the name of the array will be sizeOfArray?
No, my comment creates a variable "newArray". Original post has no variable with array. There are no line-breaks in comments so I had to put it in 1 line.
1
var arrSize = 4;
var myArray = new Array(arrSize);

or you can declare the array without size and then push a new element in it:

var myArray = new Array();
myArray.push(ITEM);

where ITEM is replaced by a variable or string or number you want to add in the array (will be added at the end of the array)

answered Jul 30, 2010 at 8:02

Comments

0

My 2 cents. (this function takes size> 26 into account)

<script type="text/javascript">
 function buildAlphabetArray(size) {
 var range = 26;
 var arr = new Array();
 for (var i = 0; i <= parseInt(size); i++) {
 //calculate current index (recalc to number between 0 and range)
 var j = (i < (range-1) ? i : (i - range * parseInt(i / range))); 
 //get the char value of ascii 65 + index (charAt(65)==A)
 arr[i] = String.fromCharCode(j + 65); //
 }
 //test
 //alert(arr.join(""));
 return arr;
 }
</script>
answered Jul 30, 2010 at 8:22

Comments

0
var a = 5;
var arr = new Array(a);
for (var i = 0;i<a;i++) {
 arr[i] = String.fromCharCode(65+i);
 document.write("arr["+i+"] = '"+arr[i]+"'<br/>");
}
answered Jul 30, 2010 at 7:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.