-1

I'm working on a mock censorship google for history class. What is supposed to happen is a user enters a term, and the script will check for whether it is a "blocked" term or a searchable term. If the term is blocked, it will lead to a different page, and if it's searchable, it will lead to the actual page. However, with my code, the script is not picking up the blocked term. Any ideas?

Javascript:

function searchCensor() 
{
    var keyTerms = document.getElementById("search").value; 
    var blockedTerms = new Array("censorship", "democracy");
    var counter, blocked;
    for(counter = 0; counter < blockedTerms.length; counter++) {
        if(keyTerms == blockedTerms[counter])
            blocked = 1;
        else
            blocked = 0;   
}
    switch(blocked)
    {
    case 1: window.location = "http://andrewgu12.kodingen.com/history/censor.php";
    case 0: window.location = "https://www.google.com/search?q="+keyTerms;
    }   
}

website: http://andrewgu12.kodingen.com/history/

Thanks!

2
  • Are you getting any errors? What data are you testing with? Commented Apr 26, 2012 at 19:28
  • I'm testing in the blocked terms("democracy" and "censorship") and none of them are being picked up as "blocked" Commented Apr 26, 2012 at 19:30

5 Answers 5

2

Think about your loop carefully:

for(counter = 0; counter < blockedTerms.length; counter++) {
    if(keyTerms == blockedTerms[counter])
        blocked = 1;
    else
        blocked = 0;
}

It the search term is "censorship", then the first time through the loop would set blocked to 1. But the second time through the loop, it would look at "censorship" and check it against "democracy", and then set blocked to 0. This effectively only sets blocked to 1 if the last blocked term matches the query.

What you really want is to set blocked to 1 if any blocked term matches the query:

var blocked = 0;
for (var counter = 0; counter < blockedTerms.length; counter++) {
    if(keyTerms == blockedTerms[counter]) {
        blocked = 1;
        break;    // Don't need to continue checking, we know it's blocked
    }
}

Note that in general, it's more meaningful (and thus easier to read) if you use true and false for this kind of flag, instead of a number (what does it mean if blocked = 3? Nothing). The code would become:

var blocked = false;
for (var counter = 0; counter < blockedTerms.length; counter++) {
    if(keyTerms == blockedTerms[counter]) {
        blocked = true;
        break;    // Don't need to continue checking, we know it's blocked
    }
}

if (blocked) {
    // ...
}
else {
    // ...
}

Finally, if you intend to pick up censored terms within a query composed of multiple words, you can do so by splitting the search query and then checking if any of those terms are blocked (with a nested loop). Or, you could use a regular expression and eliminate the loops entirely:

var blocked =
    new RegExp('\\b' + blockedTerms.join('\\b|\\b') + '\\b', 'i')
    .test(keyTerms);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Seems like it's merely a matter of logic improvement now
1

This will only work if your LAST blocked term is matched.

You need to modify your for loop to break when it finds a match:

for(counter = 0; counter < blockedTerms.length; counter++) {
    if(keyTerms == blockedTerms[counter])
    {
        blocked = 1;
        break;
    }
    else
        blocked = 0;   
}

Comments

0

try this :

you should : stop if you find block or just raise a flag and never touch it again ( as you did)

at first i asssume :blocked=0; //not blocked

if the loop is finding that its blocked - so blocked gets '1'. and stays '1'.

function searchCensor() 
{
    var keyTerms = document.getElementById("search").value; 
    var blockedTerms = new Array("censorship", "democracy");
    var counter, blocked=0;
    for(counter = 0; counter < blockedTerms.length; counter++) {
        if(keyTerms == blockedTerms[counter])
            blocked = 1;

}
    switch(blocked)
    {
    case 1: window.location = "http://andrewgu12.kodingen.com/history/censor.php";
    case 0: window.location = "https://www.google.com/search?q="+keyTerms;
    }   
}

Comments

0

This looks like a case of switch fallthrough. your switch statement should look like this (note the break):

switch(blocked)
    {
    case 1: window.location = "http://andrewgu12.kodingen.com/history/censor.php"; break;
    case 0: window.location = "https://www.google.com/search?q="+keyTerms;
    }   

Also, The loop logic is weird, as mentioned above.

Comments

0

The problem is that even if you manage to detect the right condition, since you dont break in your switch statement, both cases will be run and you will be redirected to the last setting to window.location.

Strangely enough, window.location is not instant. You can see this for yourself in this fiddle:

http://jsfiddle.net/BFmLU/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.