0

I'm using HTML and javascript to make simple web games to play. so giving preset value as an "map element" by using arrays and library.

calling values in an array goes like this:

array = [a,b,c,d]
 blah.innerHTML = array[1] // returns "b"

or

lib = {a:"cat",b:"dog",c:[a,b,c,d]}
 blah.innerHTML = lib.c.1(or lib.c[1]) // returns "b"

shouldn't it?

well it returns as an error by saying something like "the null value is null" (i had to translate it)

it used to work for sure, but it doesn't anymore... might know why?

used Software: ie8(im in some kind of "closed society"), dreamweaver

Tamil Selvan C
20.3k12 gold badges55 silver badges72 bronze badges
asked Jun 18, 2016 at 7:04
1
  • 1
    should be lib.c[1] , also is it ['a','b','c','d'] or [a,b,c,d] ? Commented Jun 18, 2016 at 7:05

3 Answers 3

3

If a,b,c,d are strings, you should quote them:

lib = {a:"cat",b:"dog",c:['a','b','c','d']}

You need to surround the values in single or double quotes because they are string values.

answered Jun 18, 2016 at 7:07
Sign up to request clarification or add additional context in comments.

Comments

1

You array should be:

array = ['a','b','c','d']
lib = {a:"cat",b:"dog",c:['a','b','c','d']}

https://developer.mozilla.org/enUS/docs/Web/JavaScript/Guide/Grammar_and_Types#Array_literals

answered Jun 18, 2016 at 7:08

Comments

0

In first snippet array is javascript Array

array = [a,b,c,d]
 blah.innerHTML = array[1] // returns "b"

SO array[1] will return the element at index 1.

In this snippet lib is an Object & c is a key of lib

lib = {a:"cat",
 b:"dog",
 c:['a','b','c','d']}
 var _c = lib.c // will give ['a','b','c','d']
var blah = document.getElementById("blow") // id of a DOM element
blah.innerHTML = _c[1] // returns "b"

NOTE:a,b,c,d in [a,b,c,d] will try to refer some variable. Unless declared it will throw error

JSFIDDLE

mplungjan
180k29 gold badges183 silver badges246 bronze badges
answered Jun 18, 2016 at 7:16

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.