0

So im wanting to compare a number(pullNumb)(gotten by another function elsewhere) to a range of numbers in an array.

for this example, lets say:

var pullNumb = 100; 
var morn_T= new Array(6,7,8,9,10,11,12,13,14,15,16,17,18,19);

if(pullNumb>morn_T[1]){ document.write(" xyz ");

}

now instead of me writing out morn_T[1],[2],[3]; etc...is there a way to shorten this?

ideally if i could add this range to a variable that would be great. but anything better than this long way ill gladly take.

i found some page on here but...man that page was WAY to advanced for me....had other languages so i got lost.

im still learning this so any tipslinks etc, i humble accept.

thanks in advance.

Pointy
  • 405,095
  • 59
  • 585
  • 614
somdow
  • 6,268
  • 10
  • 40
  • 58
  • i would better get a programming book and learn basics, otherwise you'll spend months learning what you can in few days – fazo Sep 22 '11 at 20:43
  • how many times do you want xyz printed? once for every match that returns true? or only once if all evaluate to true? – Joseph Marikle Sep 22 '11 at 20:44
  • 1
    Get the length of morn_T, use for loop and iterate on all array index, its the simplest way to implement this. – Emaad Ali Sep 22 '11 at 20:45
  • [Found by googling "loop through javascript array"](http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript) – Dave Newton Sep 22 '11 at 20:46

4 Answers4

1

Hum, so you wan't a loop?

var morn_T= new Array(6,7,8,9,10,11,12,13,14,15,16,17,18,19);
for(var i = 0; i < morn_T.length; i++)
    alert(morn_T[i]);

Demo (will spam alert).

I would suggest you read up on JavaScript. This is one of the best resource around.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • thanks, still learning here. i tried using the for loop but it kept going wonky. i forgot the "i" in morn_T[i] part. again, thank you. – somdow Sep 22 '11 at 21:55
1

You could make a simple Range type like so:

var Range = function(a,b) {
  this.min = Math.min(a,b);
  this.max = Math.max(a,b);
}
Range.prototype.contains = function(x) {
  return ((this.min <= x) && (x <= this.max));
};
var oneThroughTen = new Range(1, 10);
oneThroughTen.contains(5); // => true
oneThroughTen.contains(0); // => false

You could add additional methods for inclusive/exclusiveness on the ends as well.

Of course, if your range is not continuous then you're best off looping through the contents of the array and comparing.

maerics
  • 151,642
  • 46
  • 269
  • 291
1

Is the array sorted at all?
Are these document.writes in another sort of string array?

If the array happens to be sorted upwards, you could do the following: (Pseudo code, my javascript isn't the best, please modify as necessary)

var i = 0;
do
{
    if(PullNumb > morn_t[i])
    {   var stringVar = " "; 
        for(var j = 0; j <= morn_t[i]; j++)
        {
            stringVar += morn_t[j];
        }
        break;
    }
    i++; 
 }
 while( PullNumb < morn_t[i] && i < morn_t.Length());

So basically if the array is sorted, loop through it. Continue until the array is no more, or until the PullNumb is greater than that array value.

If the pullnumb is greater than that array value, start from the beginning of the array and append each corresponding string value to the stringVar variable.

If, however, the array is just random numbers, and the strings (from document.write) aren't in any sort of array, I suppose using a switch statement is moderately faster.

Gobbledigook
  • 472
  • 1
  • 6
  • 20
1

The concept you want may be Array.filter https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter It doesn't exist in every browser, but the page above explains how to implement it

The following code returns an array of all the numbers that are less than pullNumb

var biggerThan = morn_T.filter( function(a){ return pullNumb < a; } );
// Now you can just iterate through the filtered array
for (var i=0; i < biggerThan.length; i ++) { console.log( biggerThan[i] ); }
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217