简体   繁体   English

我该如何缩短?

[英]How can I shortened this?

I am trying to self teach myself programming and started with javascript. 我试图自学编程,并从javascript开始。 To learn more I have been completing challenges to practice and one challenge was to write a script that would determine the first case of the word in a string with the most repeated letters. 要了解更多信息,我已经完成了练习方面的挑战,其中一个挑战是编写一个脚本,该脚本将确定字母中重复次数最多的字符串中单词的第一种情况。 I was able to complete it with this code I made: 我可以用以下代码完成此操作:

string = "Hey i believe";
string = string.split(" ");
stringarray = [];
longestlength = 0; 

for (i = 0; i < string.length; i++) {
    stringarray.push(0);
}

for (i = 0; i < string.length; i++) {
    if (string[i].length > longestlength) {
        longestlength = string[i].length;
        longestword = string[i];
    }
}

for (x = 0; x < string.length; x++) {
    y = 0;
    z = 0;
    while (z < string[x].length) {
        if (string[x].substr(z,1) == string[x].substr(y,1) && z !== y) {
            stringarray[x] += 1;
            y = string[x].length -1;
        }
        y++;
        if (y == string[x].length) {
            z++;
            y = z;
        }
    }
}

if (Math.max.apply(null,stringarray) === 0) {
    mostrptltword = -1;
}
else {
    mostrptltword = string[stringarray.indexOf(Math.max.apply(null,stringarray))];
}

console.log(mostrptltword);

But to get all the points possible for the challenge it must be completed in less than 10 minutes this took me 25 mins. 但是要获得所有挑战所需的积分,必须在不到10分钟的时间内完成,这花了我25分钟的时间。 So my question is am I over complicating things; 所以我的问题是我使事情复杂化了吗? causing me to write a much longer script than needed? 使我编写了比所需时间更长的脚本? I have read a little bit about things like Regular Expressions and how they can really shortened script lengths and the time it takes to write them would that or maybe another technique of been more useful than all the loops I had to make? 我已经阅读了有关正则表达式之类的内容,以及它们如何真正缩短脚本的长度以及编写它们所花费的时间,这也许或者是另一种比我必须做的循环更有用的技术?

var words = "Heyyyyy I believe".split(' '); // split the words into an array


var values = [],   // total of times that a letter appears
    k = 0,         // 'global' counter. I'm using this to iterate over the values array
    heigher = 0,   // holds de heigher occurrence of a letter 
    letter = "";   // the letter that most appears in that word
    word = "";     // the word


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

    // iterate over each letter in each word
    for(var j = 0; j < words[i].length; j++) {

        // holds the occurrence time
        // RegEx: get the word in the position 'i' and check how many times the letter appears on the position [j] appears
        values[k] = words[i].match(new RegExp(words[i][j],'g')).length;

        // check if the next letter appears more times than the previous one
        if(values[k] > heigher) {
            // hold the values of interest
            heigher = values[k];
            letter = words[i][j];
            word = words[i];
        }
        k++;
    } 
}

console.log("word: " + word + " letter: " + letter + " total: " + heigher);

jsfiddle: http://jsfiddle.net/felipemiosso/FyCHG/ jsfiddle: http : //jsfiddle.net/felipemiosso/FyCHG/

The is commented. 被评论。 Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM