0

I am working on an educational tool. I want to retrieve a value from one of a group of arrays based on a parameter passed to a function. Specifically, this goal of this code is to change the label text on a group of radio-buttons based on selections by a user.

Here's my code:

    //arrays
var bodyplan = ['Anguilliform', 'Compressiform', 'Depressiform', 'Filiform', 'Fusiform', 'Globiform', 'Sagittiform', 'Taeniform']

var caudalshape = ['Continuous', 'Emarginate', 'Forked', 'Lunate', 'Rounded', 'Truncate']

var mouthposition = ["Inferior", "Jawless", "Subterminal", "Superior", "Terminal"]

//function
function changelabels(opt1, opt2){
    var i = opt2
    var c = 1
    var index = 0
    while (i>=c){
        document.getElementById("rb" + c + "lbl").innerHTML = (opt1[index]) 
        c = c + 1
        index = index + 1}}

The call to the function is made in a switch statement that is quite lengthy. For example here is one of the calls:

case 4:
    changelabels("caudalshape", 6)

The code I created above does change the labels, but not in the desired way. For the above case instead of my radio-buttons being labeled "Continuous", "Emarginate", "Forked" and so on, they are labeled "c", "a", "u", "d" and so on. In other words the function I wrote is not pulling values from the array. It is simply selecting letters from the parameter word passed tot he function according to the index.

Any help would be appreciated. I'm a relative novice at Javascript. I've been trying to tackle this problem for several hours both in playing with code and in searching these forums.

WyoBuckeye
  • 187
  • 2
  • 13

3 Answers3

1

This is because you're operating on the string "caudalshape" rather than the array which is pointed to by the variable named "caudalshape".

You can fix this by passing the array rather than the string;

changelabels(caudalshape, 6)

Unrelated to your problem; you should really start adding your own semi-colons to your code. Yes, it's optional in JavaScript, but it's not in many others, and it's a pain to adjust to when the time comes to learn another language (personal experience). For more info, see Do you recommend using semicolons after every statement in JavaScript?

Community
  • 1
  • 1
Matt
  • 74,352
  • 26
  • 153
  • 180
  • Good God, I'm an idiot. Thanks. I'm still learning, but I really should have caught that one. I appreciate your help. Thanks for the tips on the semicolons. I will heed your advise. – WyoBuckeye Dec 15 '11 at 15:50
1

Unless I'm missing something in your implementation, you want to pass in the caudalshape variable rather than the string:

case 4:
    changelabels(caudalshape, 6)
Phil Klein
  • 7,344
  • 3
  • 30
  • 33
0

1st there are some redundant code, which i am removing for your refrence. and also your missing semicolon everywhere.

    //arrays
var bodyplan = ['Anguilliform', 'Compressiform', 'Depressiform', 'Filiform', 'Fusiform', 'Globiform', 'Sagittiform', 'Taeniform'];

var caudalshape = ['Continuous', 'Emarginate', 'Forked', 'Lunate', 'Rounded', 'Truncate'];

var mouthposition = ["Inferior", "Jawless", "Subterminal", "Superior", "Terminal"];

//function
function changelabels(opt1, opt2){
    var index = 0;
    while (index < opt2){
        document.getElementById("rb" + (index+1) + "lbl").innerHTML = opt1[index];
        index = index + 1;
        }
  }

Here are the correction: : the parameter you are passing is string, you should pass array variable

case 4:
    changelabels(caudalshape, 6);
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58