/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help with functions 4

Hey, Please can someone help me work out a code for this function

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

[B]Description[/B]
returns an array of indexes of each page in contents that match pattern (match in the style of function 3)

[B]Examples[/B]
matchContents(contents,”LU”) returns [0,1]
matchContents(contents,”other”) returns [0,2]

Thanks!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CodeGuruauthorNov 07.2013 — Hey, Please can someone help me work out a code for this function

[B]Name of function and its parameter[/B]

matchContents(contents,pattern)

[B]Description[/B]

returns an array of indexes of each page in contents that match pattern (match in the style of function 3)

[B]Examples[/B]

matchContents(contents,"LU") returns [0,1]

matchContents(contents,"other") returns [0,2]

Thanks![/QUOTE]


A bit silly to put this with no information of what I've done so far but here it is and I think it works well.

var contents = [ "Lancaster University offers degree programmes and world class research.", "An alternative University", "Yet another University"];

function match(string, pattern){
var string= string.toUpperCase();
var pattern= pattern.toUpperCase();
for(var k= 0; k< pattern.length; k++){
if(string.indexOf(pattern[k])<0)
return false;
}
return true; }

function matchContents(contents,pattern)
{ var index=[];
for(var i=0;i<contents.length;i++)
{
if(match(contents[i],pattern))
index[index.length]=i;
}
return index;
}

alert("["+ matchContents(contents,"LU")+"]"); //returns [0,1]
alert("["+ matchContents(contents,"other")+"]"); //returns [0,2]


If there are any adjustments I can make please let me know!
Copy linkTweet thisAlerts:
@bionoidNov 07.2013 — Congrats if you got it to work without help.

Here is the single function version:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>

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

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

<i> </i> function matchContents(contents, pattern)
<i> </i> {
<i> </i> var
<i> </i> results = [], string,
<i> </i> i, j;

<i> </i> pattern = pattern.toLowerCase();
<i> </i> for (i = 0; i &lt; contents.length; ++i) {
<i> </i> string = contents[i].toLowerCase();
<i> </i> for (j = 0; j &lt; pattern.length; ++j) {
<i> </i> if (string.indexOf(pattern.charAt(j)) &lt; 0) {
<i> </i> break;
<i> </i> }
<i> </i> }
<i> </i> if (j &gt;= pattern.length) {
<i> </i> results.push(i);
<i> </i> }
<i> </i> }
<i> </i> return results;
<i> </i> }

<i> </i> alert(matchContents(contents, 'LU')); //returns [0,1]
<i> </i> alert(matchContents(contents, 'other')); //returns [0,2]

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

<i> </i>&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@CodeGuruauthorNov 07.2013 — I did ^_^ I'm learning well! Thanks again
×

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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...