0

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;
    }
2
  • You may want to consider using an object literal instead of an array. Commented Oct 17, 2013 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. Commented Oct 17, 2013 at 6:31

5 Answers 5

0

To split an array, such as:

transArray[0] = "Food:200";

Just use split:

var newArray = transArray[0].split(':');
// newArray[0] = 'Food', newArray[1] = '200'
0

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);
}
0

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. 
   }
0
var arr = new Array();
arr[0] = "Gas:200";

var newArr = arr[0].split(':');
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.