查找字谜 JavaScript jQuery

发布于 2024-12-09 15:35:53 字数 243 浏览 0 评论 0原文

假设我有一个像

Dog 这样的列表 狗 上帝 狗 狗狗 斑马 Wood

我想做的是找到列表中的所有单词,无论大小写,或者无论实际单词如何。我想匹配字母并计数。因此,从上面开始,

Dog、dOg、God 都是匹配项,在这种情况下将返回“3”作为计数,但 Doggy、dogg、zebra、wood.. 都将是唯一的,并且都将返回 1 作为计数。虽然我知道这是可能的,但我不知道从哪里开始。字谜概念让我有点困惑。有什么想法吗?

Lets Say I have a list like

Dog
dOg
God
doggy
dogg
Zebra
Wood

What I want to do is find all the words in the list regardless of case, or regardless of the actual word. I want to match the letters and take a count. So from above

Dog, dOg, God would all be a match and in this case would return "3" as the count, but doggy, dogg, zebra, wood.. would all be unique and all would return 1 as the count.. Though I know this is possible I don't know where to begin. The anagram concept throws me off a bit. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里梦着梦中梦 2024-12-16 15:35:53
var words = new Array("Dog", "dOg", "God", "doggy", "dogg","Zebra", "Wood");
var unique = {};

// iterate over all the words
for (i=0; i < words.length; i++) {

    // get the word, all lowercase
    var word = words[i].toLowerCase();

    // sort the word's letters
    word = word.split('').sort().join('')

    // keep a count of unique combinations
    if(unique[word])
        unique[word] += 1;
    else
        unique[word] = 1;
}

// print the histogram
for (u in unique)
    document.write(u + ": " + unique[u] + "<br/>")
var words = new Array("Dog", "dOg", "God", "doggy", "dogg","Zebra", "Wood");
var unique = {};

// iterate over all the words
for (i=0; i < words.length; i++) {

    // get the word, all lowercase
    var word = words[i].toLowerCase();

    // sort the word's letters
    word = word.split('').sort().join('')

    // keep a count of unique combinations
    if(unique[word])
        unique[word] += 1;
    else
        unique[word] = 1;
}

// print the histogram
for (u in unique)
    document.write(u + ": " + unique[u] + "<br/>")
沫尐诺 2024-12-16 15:35:53

这就是我想出的... jsfiddle 这里

$(document).ready(function() {
    var mywords = ['Dog', 'dOg', 'God', 'doggy', 'dogg', 'Zebra', 'Wood'];
    var finalArr = {};

    for (var i = 0; i < mywords.length; i++) {
        var temp = mywords[i].toLowerCase();
        var letters = temp.split('');
        var sorted = letters.sort();
        var final = sorted.join("");
        if(typeof finalArr[final] != 'undefined'){
            finalArr[final] ++;
        } else {
            finalArr[final] = 1;
        }
    }
    console.log(finalArr);
    for(var i in finalArr) {
        alert(i + ': ' + finalArr[i]);
        document.write(i + ': ' + finalArr[i] + "<br/>");
    }
});

here's what I came up with... jsfiddle here

$(document).ready(function() {
    var mywords = ['Dog', 'dOg', 'God', 'doggy', 'dogg', 'Zebra', 'Wood'];
    var finalArr = {};

    for (var i = 0; i < mywords.length; i++) {
        var temp = mywords[i].toLowerCase();
        var letters = temp.split('');
        var sorted = letters.sort();
        var final = sorted.join("");
        if(typeof finalArr[final] != 'undefined'){
            finalArr[final] ++;
        } else {
            finalArr[final] = 1;
        }
    }
    console.log(finalArr);
    for(var i in finalArr) {
        alert(i + ': ' + finalArr[i]);
        document.write(i + ': ' + finalArr[i] + "<br/>");
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文