SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can I do this with a javascript loop?

    ok, I have a variable that holds the value of "bl"

    I also have an array that holds the value of "black, red, blue, green, yellow"

    I want to search the array with a loop to see which values in the array begin with "bl"

    obviously black and blue should be output

    I cannot figure this out. In PHP i could get it, but something with JS isn't working for me.
    I live in Madison. Where in the W1RLD do you live?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code Javascript:
    var colours = ['black', 'red', 'blue', 'green', 'yellow'];
    for (var i = 0, j = colours.length; i < j; i++) {
      if (colours[i].indexOf('bl') === 0) {
        // starts with 'bl'
      }
      else {
        //doesn't start with 'bl'
      }
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •