-2

Hey all im currently using a function to match pages with a pattern then returning the index of all those that match e.g.

(index1_match1(['Apple','banana','bananas','chicken','bana'],'bana')) will return (1,2,4)

at the moment i have declared an empty array and using the push function to add the data in the array is there any other ways to do this as i am not allowed to use a push function as we havnt been taught it yet

  • 3
    If you can't use the sensible option because you haven't been taught it yet, then we have to *guess* at what you have been taught and tell you based on that, otherwise any answer could have the same problem. – Quentin Nov 08 '12 at 15:37
  • possible duplicate of [Appending to array](http://stackoverflow.com/questions/351409/appending-to-array) – Bergi Nov 08 '12 at 15:39

3 Answers3

3
myArray[myArray.length] = "newvalue";
alexandernst
  • 14,352
  • 22
  • 97
  • 197
0

You can create an array in 1 line

var arr = ['Apple','banana','bananas','chicken','bana']

You can create an empty array and assign elements

var arr = new Array()
arr[0] = 'Apple';
arr[1] = 'banana';

You can tell your teacher they're an idiot, and that by reading and understanding a language rather than just repeating back to them what they've taught you shows intelligence and proactiveness. Disclaimer: Im not doing your detention for you if you take this route!

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

You could consider using filter - the code hereafter delivers a subset of the initial array based on the filter term 'bana':

var mapped = ['Apple','banana','bananas','chicken','bana']
              .filter( function(el){
                          if (/bana/i.test(el)) {
                                this[this.length] = el;} 
                          return true;
                        },[]);
//=> mapped is now ["banana", "bananas", "bana"]
KooiInc
  • 119,216
  • 31
  • 141
  • 177