Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Thanked 0 Times in 0 Posts

    Exclamation Simple array design

    Not sure if this is the right place to put this but I'm kinda stuck

    I have a task that I'd like to simplify by using js and I think I have the basic concept down.

    In a nutshell, I need to compare a long list of strings vs another list of static strings that will not change.

    I think I can accomplish this by creating an array, then having an input and then an if/else statement.

    But the issue that I'm running into is the length of the list to cross check..

    I can put the information into the array, that is not problem since it won't change but I'd like to be able to check more than one item at a time.

    To give you some sort of idea here is the code snippet from what I have.

    <script language="JavaScript">
    var software =["Test","Test2","Test3"]; 
    var name=prompt("Please input name") // Prompts for software list
     if (name.toLowerCase()=== "Test".toLowerCase()) //Returns true or false based on the prompt
    And I'm a ruby coder, this is my first time trying anything with javascript so any help is much appreciated.


  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 332 Times in 328 Posts
    if you only test the lowercase names, then it would be feasible to save the lowercase names in the test array. after that you can test, whether a given name exists in the array
    PHP Code:
    var software = ["test""test2""test3"]; 
    name prompt("Please input name");

    if (-
    === software.indexOf(name.toLowerCase()) {
    // not found
    throw "name is not in software list"// e.g. throw error*

    * - you can throw anything in JS. the Exception 'classes' are named Error
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


    Posting Permissions

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