By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

Question on execution of an array in Javascript

P: 4
I have a problem I am working on - any help is appreciated.

An array (noted as arr) contains the numbers 1,2,3,4,5

If the following JS code is run, what numbers will be stored in the array?

Expand|Select|Wrap|Line Numbers
  1. arr[2]++;
  2. arr[1] += arr[2];
  3. arr[3] = arr[3] / arr[2];
Any help is appreciated - I am lost on this one...
Jul 4 '13 #1
Share this Question
Share on Google+
5 Replies

P: 88
Hi, riplus0623

please post the whole source code...

Expand|Select|Wrap|Line Numbers
  1. var junkData = ["nr. 1", "nr. 2", "nr. 3", "nr. 4"];
  3. console.log(junkData[3]);
IF you use this setup you see the "console.log(junkData[3]);" part?

The [3] desides which "part" or "slice" of the array it's going to print out.

So in this case it's going to print out only "nr. 3".

Think of an array like this:

var "var name" = [1. "Some Text", 2. "Some Text", 3. "Some Text"];

x(hope this got you on the right track... or at least helped you a bit... )
Jul 4 '13 #2

Expert Mod 10K+
P: 12,359
If you want to know what the values will be, then run the code and output the values.
Jul 5 '13 #3

P: 88
This is the code I used:

Expand|Select|Wrap|Line Numbers
  1. var arr = [1,2,3,4,5];
  3. console.log(arr[2]++);
  4. console.log(arr[1] += arr[2]);
  5. console.log(arr[3] = arr[3] / arr[2]);
I got 3 outputs:

Jul 5 '13 #4

Expert Mod 5K+
P: 5,293
you should first do all operations and then 'output' the resulting array - then u see what it contains
Jul 5 '13 #5

P: 4
Thank you everyone for your help. I am new at this and your help is greatly appreciated, especially the explanations of how the array works.
Jul 8 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.