/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help with functions 2

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

[B]Name of function and its parameter[/B]
idxP1(contents,pattern)

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

[B]Examples[/B]
idxP1(contents,”uni”) returns 0 (assuming that contents is initialised as shown above)
idxP1(contents,”not”) returns 2

Thanks!

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@bionoidNov 06.2013 — 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)[/QUOTE]

For example:

var contents = ['page1', 'page2', 'page3'];

or

var contents = new Array('page1', 'page2', 'page3');
Copy linkTweet thisAlerts:
@CodeGuruauthorNov 06.2013 — 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.
Copy linkTweet thisAlerts:
@CodeGuruauthorNov 06.2013 — 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"));[/CODE]
Copy linkTweet thisAlerts:
@tech_soul8Nov 06.2013 — 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 [I]assignment operator[/I] (=) in your if statement instead of [I]equality operator[/I] (==). 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 ?

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;
&lt;script&gt;
var contents = ["Essex University offers degree programmes and world class research.", "An alternative University.", "Yet another University."]

<i> </i> function idxP1(contents, pattern)
<i> </i> {
<i> </i> var len = contents.length;
<i> </i> var index;

<i> </i> for (var i = 0; i &lt; len; i++)
<i> </i> {
<i> </i> var tempElem = contents[i];
<i> </i> if (tempElem.toLowerCase().indexOf(pattern) != -1)
<i> </i> {
<i> </i> index = i;
<i> </i> break;
<i> </i> }
<i> </i> else
<i> </i> index = -1;
<i> </i> }
<i> </i> return index;
<i> </i> }

<i> </i> alert (idxP1(contents,"uni"));
<i> </i> alert (idxP1(contents,"not"));
<i> </i>&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@CodeGuruauthorNov 06.2013 — 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!
Copy linkTweet thisAlerts:
@tech_soul8Nov 06.2013 — You're welcome! Good luck ?
Copy linkTweet thisAlerts:
@bionoidNov 06.2013 — 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:

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<i> </i> &lt;script type="text/javascript"&gt;

<i> </i> var contents = ["Essex University offers degree programmes and world class research.", "An alternative University.", "Yet another University."];

<i> </i> function idxP1(contents, pattern)
<i> </i> {
<i> </i> var
<i> </i> index = -1,
<i> </i> i;

<i> </i> for (i = 0; i &lt; contents.length; ++i) {
<i> </i> if (contents[i].toLowerCase().indexOf(pattern) !== -1) {
<i> </i> index = i;
<i> </i> break;
<i> </i> }
<i> </i> }
<i> </i> return index;
<i> </i> }

<i> </i> alert(idxP1(contents, "uni"));
<i> </i> alert(idxP1(contents, "not"));

<i> </i> &lt;/script&gt;

<i> </i>&lt;/body&gt;
&lt;/html&gt;


What am I babbling about you say? http://www.jslint.com/
Copy linkTweet thisAlerts:
@tech_soul8Nov 06.2013 — As it is clearly forum policy, and tech_soul8 is probably on my short revenge list >?
[/QUOTE]


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 ?
Copy linkTweet thisAlerts:
@CodeGuruauthorNov 06.2013 — 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
Copy linkTweet thisAlerts:
@bionoidNov 06.2013 — Sure...
Copy linkTweet thisAlerts:
@rootNov 07.2013 — 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.
Copy linkTweet thisAlerts:
@CodeGuruauthorNov 07.2013 — 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.[/QUOTE]


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.
Copy linkTweet thisAlerts:
@tech_soul8Nov 07.2013 — ...and I thought that education sucks only in Croatia...
×

Success!

Help @CodeGuru spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...