www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Help with functions

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    27

    resolved [RESOLVED] Help with functions

    Hey, please i need help with a project im working on. Its about functions and i don't quite understand it.

    *Name of function and its parameter*
    index(string,pattern,caseSensitive)

    *Description*
    returns the index where pattern starts in string (or -1 if not found). The search is to be case sensitive if the 3rd parameter is true otherwise it is case insensitive.

    *Examples*
    index("abAB12","AB",true) returns 2 but index("abAB12","AB",false) returns 0
    index("abAB12","BA",true) returns -1 and index("abAB12","BA",false) returns 1

    Thanks!

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Presumably it's asking you to write a function called index that meets the stated specification.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Nov 2013
    Posts
    27
    I know but my function isnt working.

    function index(string,pattern,caseSensitive)
    {
    if (caseSensitive === false)
    {
    string = string.toLowerCase();
    pattern = pattern.toLowerCase();

    }
    return string.indexOf(pattern);
    alert(string.indexOf(pattern));
    }
    index("abAB12","AB",true);
    index("abAB12","AB",false);
    index("abAB12","BA",true);
    index("abAB12","BA",false);
    Q2
    function idxP1(contents,pattern)
    {
    for(var a = 0;a < contents.length;a++)
    {
    if (index(contents[a],pattern,false) > -1)
    return a;
    }
    return -1;
    }
    alert (idxP1(contents,"uni"));
    // returns 0 (assuming that contents is initialised as shown above)
    alert(idxP1(contents,"not"));
    //returns 2

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    You should have shown your code in the first place. A return statement terminates a function.
    The use of the term pattern suggested that a regex solution might be required:
    Code:
    function index( str, patt, cs )
    {
      return str.search( new RegExp( patt, ( !cs ? 'i' : '' ) ) );    
    }
    
    alert( index("abAB12","AB",true) );
    alert( index("abAB12","AB",false) );
    alert( index("abAB12","BA",true) );
    alert( index("abAB12","BA",false) );
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    The first function is actually working, but you're not doing anything with the return results:

    Code:
    function index(string,pattern,caseSensitive)
    {
    	if (caseSensitive === false) {
    		string = string.toLowerCase();
    		pattern = pattern.toLowerCase();
    	}
    	return string.indexOf(pattern);
    }
    
    alert(index("abAB12","AB",true));
    alert(index("abAB12","AB",false));
    alert(index("abAB12","BA",true));
    alert(index("abAB12","BA",false));
    JavaScript: Learn | Validate | Compact | bionoid

  6. #6
    Join Date
    Nov 2013
    Posts
    27
    Thank you so much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles