473,770 Members | 6,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on execution of an array in Javascript

4 New Member
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
5 1339
Xillez
93 New Member
Hi, riplus0623

please post the whole source code...

Expand|Select|Wrap|Line Numbers
  1. var junkData = ["nr. 1", "nr. 2", "nr. 3", "nr. 4"];
  2.  
  3. console.log(junkData[3]);
  4.  
IF you use this setup you see the "console.log(ju nkData[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
Rabbit
12,516 Recognized Expert Moderator MVP
If you want to know what the values will be, then run the code and output the values.
Jul 5 '13 #3
Xillez
93 New Member
This is the code I used:

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

3
6
1
Jul 5 '13 #4
gits
5,390 Recognized Expert Moderator Expert
you should first do all operations and then 'output' the resulting array - then u see what it contains
Jul 5 '13 #5
riplus0623
4 New Member
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

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

Similar topics

2
5234
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it down to a very simple test case which can be cut-n-pasted into a .html file and viewed in a browser: ============================================================================
6
3670
by: Flip | last post by:
I'm reading the O'Reilly's Progamming C# book and I have a question about array bounds checking. On page 174, near the top, they show an example where c# does indeed to array bounds checking cause the example shows a System.IndexOutOfRangeException being thrown. However, in the very next section (Jagged arrays) they have warning section that says "Java programmers take note: While Java does bounds checking on array use, C# does not." ...
3
1272
by: Diana M | last post by:
Hello, I have started my first asp.net application (beginner). I have 2 text boxes on the form that should contain 2 different dates (beginning and end). It would be nice to have 2 small buttons besides each text box that allow to open little form with calendar. I came from VB world where we had arrays of controls, but here I can't make it. Should I have 2 controls with different names and write code twice in the button click event or...
2
1345
by: sclemens420 | last post by:
On the PHP site on the preg_replace() function page ( http://us3.php.net/manual/en/function.preg-replace.php ), example 1 uses "@" signs in the regular expression members of the "search" array. On another PHP site ( http://www.phpfreaks.com/phpmanual/page/function.preg-replace.html ), Example 5 is the same script except the regex array members use double- then single-quotes. Why?
2
1926
by: Water Cooler v2 | last post by:
http://www.w3schools.com/js/js_whereto.asp This link is to a JavaScript tutorial on w3schools. The page says that a script put in the HEAD is executed only when called, whereas one put in the BODY is executed as the page loads (implying invariably, whether called or not). To test, I tried the following two pages:
3
3714
by: zarroba | last post by:
Hi, I'm developing a webpage that is composed by several divs. These divs are supplied by the server depending on the user that made the request. Some of these divs require some javascript functions to be called when they are added to the page. I add the content using innerHTML (function renderPage(data) { document.getElementsByTagName('body').innerHTML = data; }). (data: <div id="id" style="...."> .....
1
958
by: Nick | last post by:
Hi, I have a question about array in vc++. I initialized an array from 0 to 480 using memset. and I just realized that in one instance of my code i am putting a value in array position -2, which the array does not have. will this action cause a problem if i try to use delete to deallocate the array from memory?? thank you in advance! help is greatly appreciated!
4
2206
by: Markus Ernst | last post by:
Hi While looping through an array I extract all keys on the fly, as in this example: $data = array( array('name' ='Helen', 'age' ='40'), array('name' ='Mark', 'email' ='mark@xyz.com', 'age' ='90'), ); $keys = array();
8
7444
by: linuxfedora | last post by:
Which one is faster or any other better way to do it. I have an array of byte with name: sendBuffer, and i will like to make some thing like that the value started from index of the array in realDataSent is now copy to the beginning of the sendBuffer. I have tried: Array.Copy(sendBuffer, realDataSent, sendBuffer, 0, sendBuffer.Length - realDataSent);
7
1937
by: RubyRedRick | last post by:
I bought Crockford's "JavaScript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() { var slice = Array.prototype.slice, args = slice.apply(arguments), that = this;
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.