【问题标题】:Cant figure out search error无法弄清楚搜索错误
【发布时间】:2013-10-06 02:02:42
【问题描述】:

也在做这件事。我已经修复了拼写和(我认为)括号错误。还修复了我看到的几个突出的错误,但并没有走得太远。我仍然不知道下一步该去哪里。

(function(){

// Variable initialization (DO NOT FIX ANY OF THE BELOW VAR's)
var resultsDIV = document.getElementById("results"),
    searchInput = document.forms[0].search,
    currentSearch = ''
    ;

// Validates search query
var validate = function(query){

    // Trim whitespace from start and end of search query
    while (query.charAt[0] === " "){
        query = query.substring(1, query.length);
    };
    while (query.charAt(query.length-1) === ""){
        query = query.substring(0, query.length - 1);
    };

        // Check search length, must have 3 characters
        if (query.length < 3){
            alert ("Your search query is too small, try again.");

            // (DO NOT FIX THE LINE DIRECTLY BELOW)
            searchInput.focus();
            return;
        };

        search (query);
    };

    // Finds search matches
    var search = function (query){

    // split the user's search query string into an array
    var queryArray = query.join(" ");

    // array to store matched results from database.js
    var results = [];

    // loop through each index of db array
    for(var i=0, j=db.length; i<j; i++){

        // each db[i] is a single video item, each title ends with a pipe "|"
        // save a lowercase variable of the video title
        var dbTitleEnd = db[i].indexOf('|');
        var dbitem = db[i].tolowercase().substring(0, dbTitleEnd);

        // loop through the user's search query words
        // save a lowercase variable of the search keyword
        for(var ii=0, jj=queryArray.length; ii<jj; ii++){
            var qitem = queryArray[ii].tolowercase();

            // is the keyword anywhere in the video title?
            // If a match is found, push full db[i] into results array
            var compare = dbitem.indexOf(qitem);
            if(compare !== -1){
                results.push(db[i]);
            };
        };
    };

    };

        results.sort();

            // Check that matches were found, and run output functions
            if(results.length = );{
                noMatch();
            }else{
                showMatches(results);
            };


        // Put "No Results" message into page (DO NOT FIX THE HTML VAR NOR THE innerHTML)
        var noMatch = function(){
            var html = ''+
                    '<p>No Results found.</p>'+
                    '<p style="font-size:10px;">Try searching for "JavaScript".  Just an idea.</p>'
                ;
            resultsDIV.innerHTML = html;
        };

        // Put matches into page as paragraphs with anchors
        var showMatches = function(results){

            // THE NEXT 4 LINES ARE CORRECT.
            var html = '<p>Results</p>',
                title,
                url
                ;

            // loop through all the results search() function
            for(var i=0, j=results.length; i<j; i++){

                // title of video ends with pipe
                // pull the title's string using index numbers
                titleEnd = results[i].indexOf('|');
                title = results[i].subString(0, titleEnd);

                // pull the video url after the title
                url = results[i].substring(results[i].indexOf('|')+1, results[i].length);

                // make the video link - THE NEXT LINE IS CORRECT.
                html += '<p><a href=' + url + '>' + title + '</a></p>';
            };
            resultsDIV.innerHTML = html; //THIS LINE IS CORRECT.
        };

        // The onsubmit event will be reviewed in upcoming Course Material.
        // THE LINE DIRECTLY BELOW IS CORRECT
        document.forms[0].onsubmit = function(){
            var query = searchInput.value;
            validqte(query);

            // return false is needed for most events - this will be reviewed in upcoming course material
            // THE LINE DIRECTLY BELOW IS CORRECT
            return false;
            ;

        })();

【问题讨论】:

  • 你忘记了开头的 $ 标记
  • 您到处都是拼写错误和缺少花括号。有效的qte(查询);
  • 您可以在 Web 控制台上检查您的拼写错误。尝试使用它:D
  • 我正在使用 Web 控制台和 jshint.com,但没有什么能解决这个问题,我已经发现了错字,但它仍然无法工作,我尝试在某些地方放置花括号,我认为需要它们仍然没有任何修复我的问题
  • @edisonthk - $ 不需要,最外面的函数不应该是对 jQuery 的调用,它只是创建私有范围。 Joshua - 如果您修复了拼写错误和花括号问题,请单击“编辑”并将修复的版本添加到您的问题中,这样这些错误就不会分散我们对真正问题的注意力。

标签: javascript


【解决方案1】:

有一些语法错误应该会阻止它运行。

// syntax error
var results = ();

// should be
var results = [];

另外,showMatches 的定义似乎以不匹配的右括号结尾。

鉴于代码以某种方式运行时出现了这些语法错误(可能还有其他我没有注意到的错误),(a) 明显的问题是:

if (results.length = 0) {

是的,这是一项任务,而不是比较。您只需将数组的长度设置为零,就可以有效地清除它。这两者都未通过此处的长度测试(返回 0),然后将现在为空的数组关闭以显示为结果,但当然它现在是长度为 0 的数组。

另一方面,可能不是实际问题,但我无能为力。 while 循环业务来修剪字符串?是的,不要那样做。使用trim。如果你需要一个 polyfill,总有:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, '');
  };
}

【讨论】:

    猜你喜欢
    • 2015-10-26
    • 2012-08-23
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多