0

I am trying to make function which accepts 7 digit code (at minimum) and then loads images/elements of character thus creating a character from code.

I wrote this code:

<script type="text/javascript">
var skins = [];
skins["w"] = "http://example.com/char_elements/base.png";

var eyes = [];
eyes["b"] = "http://example.com/char_elements/eyes/blue.png";
eyes["g"] = "http://example.com/char_elements/eyes/green.png";
eyes["r"] = "http://example.com/char_elements/eyes/red.png";

var hair = [];
hair["b"] = "http://example.com/char_elements/hair/black.png";
hair["w"] = "http://example.com/char_elements/hair/blond.png";
hair["s"] = "http://example.com/char_elements/hair/brown.png";

var mouth = [];
mouth["h"] = "http://example.com/char_elements/mouth/happy.png";

var pants = [];
pants["s"] = "http://example.com/char_elements/pants/shorts.png";

var shoes = [];
shoes["b"] = "http://example.com/char_elements/shoes/black.png";

var torso = [];
torso["s"] = "http://example.com/char_elements/torso/shirt.png"

function LoadChar(code){
var data = code.split(/(?=[a-z])/i);
var skins = [skins[data[0]], eyes[data[1]], hair[data[2]], mouth[data[3]], pants[data[4]], shoes[data[5]], torso[data[6]]];

document.getElementById("out").innerHTML = skins.toString();
}
</script>
<div id="out"></div>

However, when I call LoadChar("wgbhsbs");

It thorws me "Cannot read property 'w' of undefined"

I am still new to Arrays in JS, so I am sure I am making some simple Syntax error or doing something a bit wrong.

So what this does is:

1) Accepts code and splits it into 7 elements (always 7, nothing else) stores it in "data" variable as array. 2) Builds "skins" array by loading: skin[number from code] and same for other elements. 3) For testing purposes I am trying to output skins array, but it stops at step 2.

Is there any explanation to this?

4
  • 4
    You're creating properties to the arrays, not indices. Commented Feb 20, 2015 at 18:20
  • 2
    arrays should not use letters or strings as keys. Commented Feb 20, 2015 at 18:20
  • Except urls, yes it's same. Commented Feb 20, 2015 at 18:24
  • 1
    Your real issue is that you define skins twice, once outside the function, and then a local variable inside the function with the same name, that doesn't have a w property Commented Feb 20, 2015 at 18:25

2 Answers 2

5

You are recreating the previously defined skins variable.

Use a different name for the variable inside the function, and you'll be fine.

var skins = [];
skins["w"] = "http://example.com/char_elements/base.png";

var eyes = [];
eyes["b"] = "http://example.com/char_elements/eyes/blue.png";
eyes["g"] = "http://example.com/char_elements/eyes/green.png";
eyes["r"] = "http://example.com/char_elements/eyes/red.png";

var hair = [];
hair["b"] = "http://example.com/char_elements/hair/black.png";
hair["w"] = "http://example.com/char_elements/hair/blond.png";
hair["s"] = "http://example.com/char_elements/hair/brown.png";

var mouth = [];
mouth["h"] = "http://example.com/char_elements/mouth/happy.png";

var pants = [];
pants["s"] = "http://example.com/char_elements/pants/shorts.png";

var shoes = [];
shoes["b"] = "http://example.com/char_elements/shoes/black.png";

var torso = [];
torso["s"] = "http://example.com/char_elements/torso/shirt.png"

function LoadChar(code){
  var data = code.split(/(?=[a-z])/i);
  var other_variable = [skins[data[0]], eyes[data[1]], hair[data[2]], mouth[data[3]],   pants[data[4]], shoes[data[5]], torso[data[6]]];

  alert(other_variable.toString());
}
LoadChar("wgbhsbs");

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

3 Comments

Well, that solved all the problems. I knew it's something small, I need to be a bit more careful with namings. Didn't even notice naming two variables same names. Thank you.
Also do I need to change all array elements to {} as mentioned in other answers?
@arleitiss - You don't need to... but you should
1

You're defining properties of an object. Not an array.

Change

var skins = [];
skins["w"] = "http://example.com/char_elements/base.png";

to

var skins = {};
skins.w = "http://example.com/char_elements/base.png";

Also you're redefining skins inside your LoadChar function as LcSalazar pointed out.

Fiddle

5 Comments

Although more appropriate, it won't make any difference. Arrays are object and can have properties indexed by strings just like any other object.
So do I access skins/eyes/hair elements like skins.data[0] is that right?
@arleitiss: No, you'd still access as skins["w"]
@arleitiss check the Fiddle
@Jonathan Checked, modified my code now. Thanks for contributing to solution.

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.