-2

This seems so simple and I honestly can't see why this isn't working. Looked at other answers on here and mine still isn't working.

var randomArray = new Array();

randomArray[0] = 859;

alert(randomArray[0]);

How come nothing happens? It's probably an easy answer but I can't work it out.

Also, I'm trying to work out how to put a random number in the array so rather than putting 859 in index 0 lets say I want to put a random number between 1 and 20 in, so far I've got this but thats not working either

randomArray[1]=Math.floor(Math.random()*3);

EDIT: the .toString on alert seemed to fix it, thanks guys, knew it would be something small!

14
  • 2
    Well have you checked the browser's developer console to see if any errors are being reported? Commented Dec 19, 2013 at 15:25
  • 1
    No errors on safari :(. If I do randomArray[] = 834 does it put it at index 0? Or 834? Danny - Yeah I was just demonstrating what code I'm using, ill change it to 20 before I forget! :) Commented Dec 19, 2013 at 15:29
  • 1
    @DanielLisik that is a syntax error in JavaScript. Commented Dec 19, 2013 at 15:30
  • 3
    What exactly does your <script> tag look like? Commented Dec 19, 2013 at 15:32
  • 4
    @user3119721 there's no possible way that your problem stemmed from the lack of a .toString() call. Absolutely no way. Commented Dec 19, 2013 at 15:35

3 Answers 3

0

Is your javascript being referenced properly in a script tag with the correct type set?

<script type="text/javascript" ...> </script>

If not, it's fully possible your browser is simply ignoring it.

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

Comments

0

This is a wild guess, because your question doesn't contain enough information to know for sure, but make sure that your code is in a <script> block that looks like this:

<script>
  // your code
</script>

Get rid of any "type" or "language" attribute on the tag; they're not needed and they're a source of error. If the "type" value is mispelled, then the browser will completely ignore the code.

Comments

-1

Try calling .ToString() on your array property.

alert(randomArray[0].toString());

5 Comments

alert calls toString internally for you.
I know, but browsers are funny sometimes so I always specify when the content is not a string.
Browsers aren't "funny sometimes". These things work predictably.
There's still no harm in toString and that has fixed unexpected behavior for me in the past.
Plus it appears the asker has said that it worked. Go figure. EDIT I guess it was an error else where in his script.

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.