0

I'm trying to use the code below to make the arrayGroup contain the data set relevant to keystrokes up or down. Depending on this it will add or subtract one from the variable named numberArray1 and allow different arrays to be called depending on which data set arrayGroup is being set to. Currently I can call the arrays as strings, but I am having trouble calling them as arrays and passing them into arrayGroup.

Your advice would be greatly appreciated!!!

var arrayGroup = [];

var numberArray1 = [1,2,3,4,5,6,7,8,9,10];

var numberArray2 = [10,11,12,13,14,15,16,17,18,19];

x=0;
alert(x);
document.addEventListener('keydown', function (evt) {
    if (evt.keyCode === 38) {
        alert('The "UP" key is being held down...?');
        alert(numberArray1);
        x=x+1;
        alert(x); 
        alert ("numberArray"+(x));
        arrayGroup = "numberArray"+(x);
        alert(arrayGroup);


    }
    if (evt.keyCode === 40) {
        alert('The "DOWN" key is being held down...?');
        arrayGroup = "numberArray"+(x-1);
        x=x-1;
        if(x<0){
            x=0;
            arrayGroup = "numberArray0";
        }

        alert(x);
        alert(arrayGroup);
    }
});

Update!!! - I am still having trouble with this...

Hello,

The purpose of the code below is to update an SVG map with RGB values relative to the data taken from the numberArrays. Every time the user uses the cursor I want to cycle through different arrays, which in turn will allow the map to change colour. Currently I am having difficulty with the global variable arrayGroup, I am unable to make the rest of the code interpret the updated arrays on keypress.

Any advice that can be offered would be greatly appreciated.

Many thanks!!!

var rsr = Raphael('map', '595.28', '841.89');

counties = [];

//obviously, this is the array storing the numbers of students for this particular year
numberArray0 =     [46,54,38,125]:    
//this empty array will store the different number categories i.e. 0-50, 51-100...
categories = [];

var arrayGroup = [];

numberArray1 = [460,200,384,135]:  

numberArray2 = [260,100,584,335]:

x=0;

if(x===0){
    arrayGroup = numberArray0;
    alert(arrayGroup);}

document.addEventListener('keydown', function (evt) {
if (evt.keyCode === 38) {
    alert('The "UP" key is being held down...?');
    x+=1;
    arrayGroup = eval ("numberArray"+(x));
    alert(arrayGroup);


}
if (evt.keyCode === 40) {
    alert('The "DOWN" key is being held down...?');
    arrayGroup = eval ("numberArray"+(x-1));
    x-=1;
    alert(arrayGroup);
        if(arrayGroup===numberArray+(-1)){
        x=0;
        alert(numberArray0);}
}
});

//this for loop populates the "categories" array with these categories, going up in       fifties (i=0; i<61 becuase 60*50=3000, the number we were taking as the highest
for(i=0; i<61; i++){
if(i==0){
categories[0]=0;   
}
else{
categories.push(categories[i-1]+50);
}
};

//empty array that will store the strings that define the colour of the counties
colourStrings=[];

//this loop goes through the population numbers for the county and finds out what category    they are.
for(var i=0; i<arrayGroup.length; i++){
for(var j=0; j<categories.length; j++){
// this is a loop within a loop. First it takes a population number and runs through all     the categories to see which one it's in
if(arrayGroup[i]>categories[j]){
//if it's bigger than the start point of the category(e.g. 71 is bigger than 50, goes in      the 51-100 bracket) we take the index 
index=j;
}


}
//the higher the category the population is in, the higher the index will be and the more     the g and b values will be reduced
//the colour value wll then be stored in colourStrings at an index that corresponds with     the county
colourStrings.push("rgb(255,"+(250-4*index).toString()+","+ (250-4*index).toString()     +")");
}

1 Answer 1

1

You are assigning a string value to the variable arrayGroup which is why you are able to access them as strings. If you want to access them as arrays, just assign the array you want to the arrayGroup variable like this:

arrayGroup = numberArray1;
console.log(arrayGroup);  // outputs: [1,2,3,4,5,6,7,8,9,10]

This way, arrayGroup[0] will output 1. The way you are currently doing it is:

arrayGroup = "numberArray1";
console.log(arrayGroup[0]); // outputs: "n" because "n" is at index 0 of the string assigned to arrayGroup.

Also, a little advice on incrementing and decrementing the value of x:

x++; // increases the value of x by one
x += 1;  // is the same as x = x + 1
x--; // decreases the value of x by one
x -= 1; // is the same as x = x -1

I think this answers your question (as best as I can understand it). Please let me know if this is not what you're looking for and I'll see if I can help further. I hope this help!

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

1 Comment

I appreciate your comments and have made note of them, but I think my problem is a little more complex. Please see my updated code if you get the chance. Thanks again...

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.