Javascript未知错误 [英] Javascript unknown error

查看:117
本文介绍了Javascript未知错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码出现问题,过去2天我无法弄明白。

I am having a problem with my code, I have not been able to figure it out for the past 2 days.

 /**
 * 
 *///function 7
/**
 * returns the number of times that pattern occurs in string 
 */
    function score(string,pattern) {
        var v = string.toUpperCase();
        var s = pattern.toUpperCase();
        var result = [];
        for (var i = 0; i < string.length; i++) {
            var index = v.indexOf(s, i);
            if (index != -1) {
                result[result.length]=index;
                i = index;
            }
        }
        return result.length;
    }

    /**
     * returns an array of records of the form {trackTitle: ..., trackLyrics: ..., trackScore: ...} derived from web. 
     * Each record contains the track title, track lyrics and pattern score of its corresponding content.
     * 
     * 
     * 
     */

    //FUNCTION 9

    function urlScores(music, pattern) { 
        var scoresArray = [];
        for(var i = 0; i < music.length; i++) {
            for(var j = 0; j < music[i].tracks.length; j++){
                var itemScore = score(music[i].tracks[j].title, pattern) + score(music[i].tracks[j].lyrics, pattern);


                if (itemScore > 0) {
                    scoresArray[scoresArray.length] = ({indexOfTrack: j, trackTitle: music[i].tracks[j].title, trackLyrics: music[i].tracks[j].lyrics, trackScore: itemScore, album: music[i]});
                }
                itemScore = 0;
            }
        }
        return scoresArray;
    }
    /**
     * Sorts the result of urlScores() into descending order.
     * Records with a score of zero are omitted.
     */

    //FUNCTION 10

    function rankedScores(music, pattern) { 
        var scoresArray = urlScores(music, pattern);  

        function swap(a, b) {
            var temp = scoresArray[a];
            scoresArray[a] = scoresArray[b];
            scoresArray[b] = temp;
        }

        for(var i = 0; i < scoresArray.length; i++) {
            for(var x = 0; x < scoresArray.length - 1; x++) {

                if (scoresArray[x].score > scoresArray[x + 1].score) {
                    swap(x, x + 1);
                }
            }
        }
        alert(scoresArray);
    }

当我使用以下内容运行它时:

When I run it with the following:

  rankedScores(albums, "sparrow");

相册变量为 - http://pastebin.com/G25SxrwY

错误如下 -

  [object Object],[object Object]

非常感谢!

推荐答案

这不是错误:
如果你对包含两个对象的数组进行字符串化,这是假定的输出。

That is not an error: If you stringify an Array containing two objects, that is the supposed output.

这篇关于Javascript未知错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆