1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Array usage in Javascript

Discussion in 'JavaScript' started by Philosophaie, Jun 8, 2012.

  1. #1
    I can not seem to get this to work. It is the array:

    function this(){
       var a = [3,2,2,3,4,1];
       var b = [1,2,2,1,1,2];
       var c = [34,21,20,12,30,25];
       for (k = 0; k <= 5; k++){
            var sumR1 = a[k] * b[k] + c[k] ;
            var sumR = sumR + sumR1;
        }//Next k
        var Req = (385000.56 + sumR / 10);
        window.document.myform.sum1.value = Req;
    }
    Code (markup):
     
    Last edited: Jun 8, 2012
    Philosophaie, Jun 8, 2012 IP
  2. igunz

    igunz Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    array initialization in javascript not like that.
    try this
    
    
    function this(){
    var a = new Array(3,2,2,3,4,1);
     var b = new Array(1,2,2,1,1,2);
    var c = new Array(34,21,20,12,30,25);
    for (k = 0; k <= 5; k++){
         var sumR1 = a[k] * b[k] + c[k] ;
         var sumR = sumR + sumR1;
    }//Next k
    var Req = (385000.56 + sumR / 10);
    window.document.myform.sum1.value = Req;
     }
    
    Code (markup):
     
    igunz, Jun 8, 2012 IP
  3. FilmFiddler

    FilmFiddler Greenhorn

    Messages:
    54
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    23
    #3
    Two main problems:

    1. the accumulator variable 'sumR' is being re-declared within each loop, so it can never accumulate across loops.

    2. 'this' is a Reserved Word in JavaScript, and should not be used as a function name or a variable.

    It also seems the variables sumR1 and Req are not necessary there. See how the following works.
    I changed the name of the function to doThis(); don't forget to modify the function call accordingly.

    BTW, there's nothing at all wrong with declaring JavaScript arrays with square brackets. Many prefer to do it that way.

    function doThis(){
       var a = [3,2,2,3,4,1];
       var b = [1,2,2,1,1,2];
       var c = [34,21,20,12,30,25];
       var sumR = 0; 
       for (var k = 0; k <= 5; k++){
            sumR += a[k] * b[k] + c[k] ;
       }//Next k
       document.myform.sum1.value = (385000.56 + sumR / 10);
    }
    Code (markup):
     
    FilmFiddler, Jun 9, 2012 IP
  4. Ev0Lv

    Ev0Lv Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    nicely done. I can't seem to get the question but now that i have read your answer i am enlighten. thanks!
     
    Ev0Lv, Jun 17, 2012 IP
  5. XP1

    XP1 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Stay away from `Array` constructor syntax.

    Especially for novices, it may lead to confusion between:
    new Array(element0, element1, ..., elementN)
    new Array(arrayLength)
    Code (markup):
     
    XP1, Jun 27, 2012 IP
  6. Irfi0009

    Irfi0009 Banned

    Messages:
    17,584
    Likes Received:
    33
    Best Answers:
    1
    Trophy Points:
    48
    #6
    can you try this cod plz and tell me function this(){ var a = new Array(3,2,2,3,4,1); var b = new Array(1,2,2,1,1,2); var c = new Array(34,21,20,12,30,25); for (k = 0; k
     
    Irfi0009, Jul 11, 2012 IP
  7. charmtolucky

    charmtolucky Member

    Messages:
    293
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    35
    #7
    you might be missing something... recheck it
     
    charmtolucky, Jul 24, 2012 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    1) You're not declaring sumR before the loop, so christmas knows what it's initial value is.

    2) Avoid declaring variables you don't actually need.

    3) try using prefixed additions, it's less code.

    4) accessing form elements by name like that is nyetscape 4 style coding... since you're stuffing it into a form element, it should have a perfectly good ID on it -- if for no other reason than something it's LABEL should be pointing at.

    
    function this() {
    	var a=[ 3, 2, 2, 3, 4, 1];
    	var b=[ 1, 2, 2, 1, 1, 2];
    	var c=[34,21,20,12,30,25];
    	var sumR=0;
    	for (k=0; k<6; k++) sumR+=a[k]*b[k]+c[k];
    	document.getElementById('sum1').value=(385000.56+sumR/10);
    }
    
    Code (markup):
    Where 'sum1' is the target form element's ID.

    I'd also consider making that a single array, so you can auto-detect length instead of hardcoding it.

    
    function this() {
    	var data=[
    		[3,1,34],
    		[2,2,21],
    		[2,2,20],
    		[3,1,12],
    		[4,1,30],
    		[1,2,25]
    	];
    	var sum=0;
    	for (k=0; k<data.length; k++) sum+=data[k][0]*data[k][1]+data[k][2];
    	document.getElementById('sum1').value=(385000.56+sum/10);
    }
    Code (markup):
     
    deathshadow, Jul 27, 2012 IP