SitePoint Sponsor

User Tag List

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

    string comparison and extraction

    Hi All

    I have a problem with Javascript, I need to do something fairly complicated and I'm not sure how.
    first let me explain what I am trying to do.
    I need a user to be able to enter a single word in a text form on a page. i then need to seperate every character in that string to create new variables (example: var charater1 = string.charAt(0)) there are ten characters in total.
    I then need to compare the character to find the appropriate integer number for it, for example A would equal the integer number 1.
    once I have converted all ten characters I then need to write them to a file, something like a text file or the like.

    does anyone know how to do this? really need help

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a number of ways to handle this. It appears as if you already figured out a way to parse out each character in the string..now you're trying to get an integer. Why not create an associative array for the alphabet?
    Code:
    var alphabet = {
    a:1,
    b:2,
    c:3,
    d:4,
    ...
    z:26};
    Then you can access the number directly:
    Code:
    alphabet["a"], alphabet["b"], etc
    If you're dynamically parsing user input, then you could loop through the user's string and pull out each character's code in the loop.

    HTH


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
  •