/    Sign up×
Community /Pin to ProfileBookmark

changing the if statement

the code below is for the following example:
Write a program to simulate some aspects of a search engine.

The data to be searched will be represented by an array of pages. Each page will be represented by a string containing a URL, then a colon, then the readable content of the page in plain text.

var pages=[
“www.website.com: The internet is where this website lives. “,

];

function find_s(w) – to return an array of scores for each string in the pages array. [Score 0 for string not found, 1 for string found once, 2 for string found twice, etc. Try to ensure that your program does not find w more than once in each word – e.g. if w=’w’ then it should be found twice in ‘www:hello world’, not four times. ]

var search_term = prompt(‘Type in search term: ‘,’Search word’);
function find_s(w) {
var scores = [] var page_score = 0 for (var i=0;i<pages.length;i++) {
for (var c=0;c<pages[i].length;c++) {
if (pages[i].substr(c,w.length).toLowerCase()==w.toLowerCase()) {
page_score++;
while(pages[i].substr(c,1)!=” “) {
i++;
}
}
}
scores[i]=page_score;
page_score=0;
}
return (w + ” was found in this may times in each page ” + scores);
alert (find_s(search_term));

Is there another way of matching the word the user is searching for and finding it in the strings in the array, and counting how many times it occurs?

In simple javascript?

was wondering how i might go about changing the the if statement that matches the word that the user typed into the prompt box with the actual word or string itself in the array pages, as i was told to come up with an alternative to this example. Any help would be greatly appreciated because I am a beginner programmer and am struggling with this topic because though I dont understand the examples, I cant come up with alternatives to them as a way of improving my skills in the subject.
Any help would be greatly appreciated.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@slaughtersNov 05.2008 — You can use the split function to separate each "page" into an array of words by using " " (blank) for a separator.

words = pages[i].split(" ");



Then loop through the "words" array. Use the indexOf function to see if each word contains the string you are searching for.
×

Success!

Help @outtatime 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,
)...