www.webdeveloper.com
Results 1 to 14 of 14

Thread: [RESOLVED] Help with functions 2

  1. #1
    Join Date
    Nov 2013
    Posts
    27

    resolved [RESOLVED] Help with functions 2

    Hey, Please can you help me work out a code for this question.

    Name of function and its parameter
    idxP1(contents,pattern)

    Description
    returns the index of the first page in contents that matches pattern (case insensitive)
    returns -1 if no matching page found


    Examples
    idxP1(contents,"uni") returns 0 (assuming that contents is initialised as shown above)
    idxP1(contents,"not") returns 2

    Thanks!

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    As with this question and a few others you are posting on the forum, you seem to be missing a component that you need in order to solve the problem.

    That component is "contents". Now I'm guessing that is a static array of sorts listed in your textbook somewhere. Once you find that everything else might become clear.

    (assuming that contents is initialised as shown above)
    For example:

    Code:
    var contents = ['page1', 'page2', 'page3'];
    or

    Code:
    var contents = new Array('page1', 'page2', 'page3');
    JavaScript: Learn | Validate | Compact | bionoid

  3. #3
    Join Date
    Nov 2013
    Posts
    27
    Yeah! that was actually given. I feel silly now. JavaScript is really not my thing and i have alot of work that is due very soon thats why im posting alot of questions on the forum.

  4. #4
    Join Date
    Nov 2013
    Posts
    27
    Hey, This is what I've done so far but it is giving me -1 & -1 instead of 0 & 2. Can you give me advice please.

    Code:
    var contents = [ "Essex University offers degree programmes and world class research.", "An alternative University", "Yet another University"] 
    
    function idxP1(contents, pattern)
    {
      if (pattern = contents[0]) {
          pattern = pattern.toLowerCase();
      }
      return pattern.indexOf(contents);
    }
    
    alert (idxP1(contents,"uni"));
    alert (idxP1(contents,"not"));

  5. #5
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    You have numerous errors in your function! First it ain't gonna check all of the elements of an array. Second you have used an assignment operator (=) in your if statement instead of equality operator (==). Third instead of converting an array element to lowercase thus making your function case insensitive you are converting pattern argument...

    This is the code you're looking for but obviously you have a lot to learn so head to the job... (friendly advice), no offense

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="UTF-8" />
    	<script>
    		var contents = ["Essex University offers degree programmes and world class research.", "An alternative University.", "Yet another University."] 
    
    		function idxP1(contents, pattern)
    		{
    			var len = contents.length;
    			var index;
    			
    			for (var i = 0; i < len; i++)
    			{
    				var tempElem = contents[i];
    				if (tempElem.toLowerCase().indexOf(pattern) != -1)
    				{
    					index = i;
    					break;
    				}
    				else
    					index = -1;
    			}
    			return index;
    		}
    
    		alert (idxP1(contents,"uni"));
    		alert (idxP1(contents,"not"));
    	</script>
    </head>
    <body>
    </body>
    </html>

  6. #6
    Join Date
    Nov 2013
    Posts
    27
    Thank you so much. I know I have a lot to learn, I've only just started JavaScript and this is what gets handed to me. I'm trying my best though!

  7. #7
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    You're welcome! Good luck

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    As it is clearly forum policy, and tech_soul8 is probably on my short revenge list >
    To make the jslint validator more sympathetic to the code I would just rewrite it as such:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<body>
    
    		<script type="text/javascript">
    
    			var contents = ["Essex University offers degree programmes and world class research.", "An alternative University.", "Yet another University."];
    
    			function idxP1(contents, pattern)
    			{
    				var
    					index = -1,
    					i;
    
    				for (i = 0; i < contents.length; ++i) {
    					if (contents[i].toLowerCase().indexOf(pattern) !== -1) {
    						index = i;
    						break;
    					}
    				}
    				return index;
    			}
    
    			alert(idxP1(contents, "uni"));
    			alert(idxP1(contents, "not"));
    
    		</script>
    
    	</body>
    </html>
    What am I babbling about you say? http://www.jslint.com/
    JavaScript: Learn | Validate | Compact | bionoid

  9. #9
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by bionoid View Post
    As it is clearly forum policy, and tech_soul8 is probably on my short revenge list >
    Not sure what are you talking about because to be honest I really never ever pay attention to the forums policy (just to lazy )...

    But, I guess I did wrong with jumping into the thread you were already replied to? If so, sorry for that, just wanted to help out to CodeGuru. I'm retreating
    Last edited by tech_soul8; 11-06-2013 at 01:19 PM.

  10. #10
    Join Date
    Nov 2013
    Posts
    27
    Hey bionoid, do you think you can take a look at "help with functions 3" ? tech_soul8 was helping me out but he's not online right now

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Sure...
    JavaScript: Learn | Validate | Compact | bionoid

  12. #12
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    The question is what do you do when the tutor in class puts you on the spot for using code you have not yet been taught or to explain your code.

    The point of going to uni is to not party but attend class and learn. Making a prejudgment I know but this is my impression of whats really going on because this pretty rudimentary stuff that I self taught with no school tutoring, I hale from the days when the only computer in school was in the computer studies class and was the size of a filing cabinet and all the processing power of a calculator and nothing existed in programming other than BASIC or ASM.

    Even now I get stumped and have to ask questions, its not a learn and know everything language, even if its 20 odd or more years old, the full extent of JavaScript can not be fully grasped until you have wandered off the track a bit and find functions developed by others and you're "Wow, I didn't know you could do that" criteria.

    So do yourself a favor, don't panic, a bit of research will take you on your way and don't expect your home work to be done for you because you will never learn by that method.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  13. #13
    Join Date
    Nov 2013
    Posts
    27
    Quote Originally Posted by \\.\ View Post
    The question is what do you do when the tutor in class puts you on the spot for using code you have not yet been taught or to explain your code.

    The point of going to uni is to not party but attend class and learn. Making a prejudgment I know but this is my impression of whats really going on because this pretty rudimentary stuff that I self taught with no school tutoring, I hale from the days when the only computer in school was in the computer studies class and was the size of a filing cabinet and all the processing power of a calculator and nothing existed in programming other than BASIC or ASM.

    Even now I get stumped and have to ask questions, its not a learn and know everything language, even if its 20 odd or more years old, the full extent of JavaScript can not be fully grasped until you have wandered off the track a bit and find functions developed by others and you're "Wow, I didn't know you could do that" criteria.

    So do yourself a favor, don't panic, a bit of research will take you on your way and don't expect your home work to be done for you because you will never learn by that method.
    I appreciate your concern but my 'tutor' will not ask how/where I got my code from because he knows his students will do anything they can to pass especially when they are given coursework they have not been taught. How is someone that has never done JavaScript before meant to do the things I've posted? Over 70% of people have not even started because they don't understand and no one will help so forgive me for going the extra mile to pass.

  14. #14
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    ...and I thought that education sucks only in Croatia...

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