反击单个字符串的字母。使用Javascript

时间:2014-09-25 17:30:12

标签: javascript

我有问题。我正在尝试创建一个返回特定字符串频率字母数的函数。这是一个例子。

var a = letter_frequency("Hello");
a["H"] == 1; a["E"] == 1; a["L"] == 2; a["A"] == undefined;

现在我有我写的代码,但我不知道如何继续前进。你能救我吗?

    function letter_frequency(s) {
    if (s === undefined) {
        return undefined;
    } else {
        var splitter = s.split()

    }
}

这里有一个测试

 test( "Frequency", function() {
    deepEqual(letter_frequency() , undefined);
    var a = letter_frequency("Hello");
    equal(a["H"] , 1, "For the String 'Hello' the number of 'H' chars should be 1");
    equal(a["E"] , 1, "For the String 'Hello' the number of 'E' chars should be 1");
    equal(a["L"] , 2, "For the String 'Hello' the number of 'L' chars should be 2");
    equal(a["O"] , 1, "For the String 'Hello' the number of 'O' chars should be 1");
    deepEqual(a["A"] , undefined, "For the String 'Hello' the number of 'A' chars should be undefined");
    var a = letter_frequency("Software Atelier III");
    equal(a["I"] , 4, "For the String 'Software Atelier III' the number of 'I' chars should be 4");
    equal(a["S"] , 1, "For the String 'Software Atelier III' the number of 'S' chars should be 1");
    equal(a["A"] , 2, "For the String 'Software Atelier III' the number of 'A' chars should be 2");
    equal(a["O"] , 1, "For the String 'Software Atelier III' the number of 'O' chars should be 1");
    equal(a["T"] , 2, "For the String 'Software Atelier III' the number of 'T' chars should be 2");
    equal(a[" "] , 2, "For the String 'Software Atelier III' the number of ' ' chars should be 2");
    equal(a["F"] , 1, "For the String 'Software Atelier III' the number of 'F' chars should be 1");
    equal(a["W"] , 1, "For the String 'Software Atelier III' the number of 'W' chars should be 1");
    equal(a["T"] , 2, "For the String 'Software Atelier III' the number of 'T' chars should be 2");
    equal(a["L"] , 1, "For the String 'Software Atelier III' the number of 'L' chars should be 1");
});

解决方案:感谢h2ooooooo

所以对于我使用的代码

function letter_frequency(s) {
if(s === undefined){
    return undefined
} else {
    var fr = {};
    for (var i = 0; i < s.length; i++) {
        var ch = s.charAt(i).toUpperCase();
        if (fr[ch]) {
            fr[ch]++;
        } else {
            fr[ch] = 1;
        }
    }
}
    return freq;
}

我从收到的所有答案中链接了一些代码。唯一的区别是对未定义问题的控制,我的练习要求控件带有未定义的单词

2 个答案:

答案 0 :(得分:1)

您可以使用.split()拆分字符串并循环显示字母:

function letter_frequency(str) {
    var letters = str.split(''),
        letterFrequency = {};

    for (var i = 0, len = letters.length; i < len; i++) {
        if (!letterFrequency.hasOwnProperty(letters[i])) {
            letterFrequency[letters[i]] = 1;
        } else {
            letterFrequency[letters[i]]++;
        }
    }

    return letterFrequency;
}

letter_frequency("Hello");
// Object {H: 1, e: 1, l: 2, o: 1}

如果你想把它全部写成大写字母(出于某些奇怪的原因),你可以使用.toUpperCase()执行以下操作:

function letter_frequency(str) {
    var letters = str.split(''),
        letterFrequency = {},
        letter;

    for (var i = 0, len = letters.length; i < len; i++) {
        letter = letters[i].toUpperCase();
        if (!letterFrequency.hasOwnProperty(letter)) {
            letterFrequency[letter] = 1;
        } else {
            letterFrequency[letter]++;
        }
    }

    return letterFrequency;
}

letter_frequency("Hello");
// Object {H: 1, E: 1, L: 2, O: 1}

答案 1 :(得分:0)

这样的东西应该实现你想要的东西(我根据你提供的示例输出,将字母作为对象的大写属性返回):

String.prototype.countLetters = function() 
{
    var arr = new Array(),
        letters = this.split('');

    for(var i = 0; i < letters.length; i++)
    {
        var letter = letters[i].toUpperCase();

        if(arr[letter] !== undefined)
        {
            arr[letter]+= 1;
        }
        else
        {
            arr[letter] = 1;
        }
    }

    return arr;
}

然后,您可以使用'Hello'.countLetters();调用此方法。给定此示例,并使用Hello的测试字符串,我们得到以下输出:

[ H: 1, E: 1, L: 2, O: 1 ]

jsFiddle Demo