Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hey guys i need some code that will split an array that holds a string which is an item and amount with the delimiter being the (:). (eg. Gas:30 ) loading the elements from the transArray into the values of hmtl texboxes for the item and amount fields Please don't be to harsh with the comments this is my first language for a type-language. Any help is appreciated!

var load = function ()
{

   mySetArray();  //Fills the transArray randomly with 1-4 items
   var item = '';
   var amount = '';

   for ( i=1; i<=transArray.length; i++)
   {
    item = 'item' + i;
    amount = 'amount' + i;
    transArray.split(":");      
   }
}

  var mySetArray = function ()
  {

 var myRandom = Math.floor((Math.random() * 100) / 25) + 1;  //a number between 1      and 4

 transArray = new Array();  //Resets the Array to empty

 if (myRandom == 1)
 {
    transArray[0] = "Food:200";
 }

 if (myRandom == 2)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
 }

 if (myRandom == 3)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
    transArray[2] = "Mortgage:1800";
 }

 if (myRandom == 4)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
    transArray[2] = "Mortgage:1800";
    transArray[3] = "Cable:130";
 }
     }

      window.onload = function ()
    {
$("load").onclick = load;
    }
share|improve this question
 
You may want to consider using an object literal instead of an array. –  thgaskell Oct 17 at 6:10
 
Seem two major problems with your code. You are spliting complete transArray instead of splitting its elements (strings) one by one. Also i think your mySetArray() is not returning your transArray to load function. I have tried to supply full answer with demo. –  Sami Oct 17 at 6:31
add comment

5 Answers

To split an array, such as:

transArray[0] = "Food:200";

Just use split:

var newArray = transArray[0].split(':');
// newArray[0] = 'Food', newArray[1] = '200'
share|improve this answer
add comment

change:

for ( i=1; i<=transArray.length; i++) {
    item = 'item' + i;
    amount = 'amount' + i;
    transArray.split(":");      
}

to

for ( i=1; i<=transArray.length; i++) {
    item = 'item' + i;
    amount = 'amount' + i;
    var splitted = transArray[i].split(":"); <-- split each item in transArray 
    console.log(splitted);
}
share|improve this answer
add comment

Here transArray is an array. You should use split on it's values i.e transArray[i].split(":");

So update your code like this :

for ( i=1; i<=transArray.length; i++)
   {
      item = 'item' + i;
      amount = 'amount' + i;
      var splittedData = transArray[i].split(":");      
      // It will give Item in 0th index and amount in 1st field. 
   }
share|improve this answer
add comment
var arr = new Array();
arr[0] = "Gas:200";

var newArr = arr[0].split(':');
share|improve this answer
add comment

JSFIDDLE DEMO

Call load function where ever you want or as it is (as i have done)

function load()
{

   transArray = mySetArray();  //Fills the transArray randomly with 1-4 items
   var item = '';
   var amount = '';

   for ( i=0; i<=transArray.length; i++)
   {
    ar = transArray[i].split(":");
    alert((i+1)+" Item="+ar[0] + " Amount="+ ar[1]); // You ca use it in your own way
   }
}
load();

  function mySetArray()
  {

 var myRandom = Math.floor((Math.random() * 100) / 25) + 1;  //a number between 1      and 4

 transArray = new Array();  //Resets the Array to empty

 if (myRandom == 1)
 {
    transArray.push("Food:200");
 }

 if (myRandom == 2)
 {
    transArray.push("Food:200");
    transArray.push("Toys:700");
 }

 if (myRandom == 3)
 {
    transArray.push("Food:200");
    transArray.push("Toys:700");
    transArray.push("Mortgage:1800");
 }

 if (myRandom == 4)
 {
    transArray.push("Food:200");
    transArray.push("Toys:700");
    transArray.push("Mortgage:1800");
    transArray.push("Cable:130");
 }
      return transArray;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.