Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Array Length Is Greater Than Number Of Array Items

Array Length Is Greater Than Number Of Array Items

Scheduled Pinned Locked Moved JavaScript
data-structureshelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Liagapi
    wrote on last edited by
    #1

    I have a table element containing 3 rows and 3 columns. In each row are 3 input elements and each element has an ID like below:

    -----------------------------------
    -- R1C1 -- | -- R1C2 -- | -- R1C3

    -- R2C1 -- | -- R2C2 -- | -- R2C3

    -- R3C1 -- | -- R3C2 -- | -- R3C3

    Users can enter a number into each input element then click submit when done. An array is used to store values of input elements in each row, so there are a total of three arrays used. I'm trying to use loops to insert the value of each input element in a given row into a specific position in an array. The problem I'm having is that even if there are fewer than 3 numbers inserted into an array, it will show that its length is 3. Below is my code

    var Row1 = []; var Row2 = []; var Row3 = [];
    var r; var textbox;

    function InsertNumbersIntoArrays(){

    for(var a = 1; a< 4; a++){
    r = 'Row' + a;

    for(var b = 1; b< 4; b++){
      textbox = document.getElementById("R"+ a + "C" + b).value;   
      for(var i= 0; i<3; i++){
         if(textbox.length != 0){
           eval(r).splice(i,0,textbox.value);
         } 
      }
    } 
     alert("length of array " + r + " is " + eval(r).length);
    

    }
    }

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups