gpt4 book ai didi

javascript - Coderbyte 上的字母计数 I JavaScript 挑战

转载 作者:行者123 更新时间:2023-11-29 21:38:26 24 4
gpt4 key购买 nike

我已经处理这个问题好几个小时了,我已经尽我所能,尽我目前的新手 javaScript 能力来解决这个挑战,但我就是无法弄清楚到底出了什么问题。我不断收到“UNEXPECTED TOKEN ILLEGAL on here: http://jsfiddle.net/6n8apjze/14/

和“类型错误:无法读取 null 的属性‘长度’”:http://goo.gl/LIz89F

我认为问题出在 howManyRepeat 变量上。我不明白为什么当 word 显然是来自 str 的单词时,我无法读取 null 的长度...

我的想法是:

word.toLowerCase().split("").sort().join("").match(/([.])\1+/g).length

...此处:Get duplicate characters count in a string

挑战:
使用 JavaScript 语言,让函数 LetterCountI(str) 获取 str参数被传递并返回第一个单词数量最多的重复的字母。例如:“今天,是有史以来最伟大的一天!”应该返回最大,因为它有 2 个 e(和 2 个 t),而且它出现在 ever which 之前有2个e。如果没有包含重复字母的单词,则返回 -1。话会以空格隔开。

function LetterCountI(str){
var wordsAndAmount={};
var mostRepeatLetters="-1";
var words=str.split(" ");

words.forEach(function(word){
// returns value of how many repeated letters in word.
var howManyRepeat=word.toLowerCase().split("").sort().join("").match(/([.])\1+/g).length;
// if there are repeats(at least one value).
if(howManyRepeat !== null || howManyRepeat !== 0){
wordsAndAmount[word] = howManyRepeat;
}else{
// if no words have repeats will return -1 after for in loop.
wordsAndAmount[word] = -1;
}
});

// word is the key, wordsAndAmount[word] is the value of word.
for(var word in wordsAndAmount){
// if two words have same # of repeats pick the one before it.
if(wordsAndAmount[word]===mostRepeatLetters){
mostRepeatLetters=mostRepeatLetters;
}else if(wordsAndAmount[word]<mostRepeatLetters){
mostRepeatLetters=mostRepeatLetters;
}else if(wordsAndAmount[word]>mostRepeatLetters){
mostRepeatLetters=word;
}
}

return mostRepeatLetters;
}

// TESTS
console.log("-----");
console.log(LetterCountI("Today, is the greatest day ever!"));
console.log(LetterCountI("Hello apple pie"));
console.log(LetterCountI("No words"));

非常感谢任何指导。谢谢你!! ^____^

最佳答案

这是工作代码片段:

/*
Using the JavaScript language, have the function LetterCountI(str) take the str
parameter being passed and return the first word with the greatest number of
repeated letters. For example: "Today, is the greatest day ever!" should return
greatest because it has 2 e's (and 2 t's) and it comes before ever which also
has 2 e's. If there are no words with repeating letters return -1. Words will
be separated by spaces.

console.log(LetterCountI("Today, is the greatest day ever!") === "greatest");
console.log(LetterCountI("Hello apple pie") === "Hello");
console.log(LetterCountI("No words") === -1);

Tips:
This is an interesting problem. What we can do is turn the string to lower case using String.toLowerCase, and then split on "", so we get an array of characters.

We will then sort it with Array.sort. After it has been sorted, we will join it using Array.join. We can then make use of the regex /(.)\1+/g which essentially means match a letter and subsequent letters if it's the same.

When we use String.match with the stated regex, we will get an Array, whose length is the answer. Also used some try...catch to return 0 in case match returns null and results in TypeError.

/(.)\1+/g with the match method will return a value of letters that appear one after the other. Without sort(), this wouldn't work.
*/

function LetterCountI(str){
var wordsAndAmount={};
var mostRepeatLetters="";
var words=str.split(" ");

words.forEach(function(word){

var howManyRepeat=word.toLowerCase().split("").sort().join("").match(/(.)\1+/g);

if(howManyRepeat !== null && howManyRepeat !== 0){ // if there are repeats(at least one value)..
wordsAndAmount[word] = howManyRepeat;
} else{
wordsAndAmount[word] = -1; // if no words have repeats will return -1 after for in loop.
}
});

// console.log(wordsAndAmount);
for(var word in wordsAndAmount){ // word is the key, wordsAndAmount[word] is the value of word.
// console.log("Key = " + word);
// console.log("val = " + wordsAndAmount[word]);
if(wordsAndAmount[word].length>mostRepeatLetters.length){ //if two words have same # of repeats pick the one before it.
mostRepeatLetters=word;
}
}
return mostRepeatLetters ? mostRepeatLetters : -1;
}

// TESTS
console.log("-----");
console.log(LetterCountI("Today, is the greatest day ever!"));
console.log(LetterCountI("Hello apple pie"));
console.log(LetterCountI("No words"));

/*
split into words

var wordsAndAmount={};
var mostRepeatLetters=0;


loop through words
Check if words has repeated letters, if so
Push amount into object
Like wordsAndAmount[word[i]]= a number
If no repeated letters...no else.

Loop through objects
Compare new words amount of repeated letters with mostRepeatLetters replacing whoever has more.
In the end return the result of the word having most repeated letters
If all words have no repeated letters return -1, ie.
*/

所做的更改:

  • [.] 变成了 . 因为 [.] 匹配文字句点符号,不是任何字符而是换行符
  • 在代码末尾添加关闭*/(最后一个注释 block 没有关闭导致UNEXPECTED TOKEN ILLEGAL)
  • if(howManyRepeat !== null || howManyRepeat !== 0) 应替换为 if(howManyRepeat !== null && howManyRepeat !== 0)因为否则 null 正在测试是否与 0 相等并导致 TypeError: Cannot read property 'length' of null" 问题。请注意 .match(/(.)\1+/g).length不能使用,因为匹配的结果可能为null,也会导致出现TypeError。
  • 获取具有最大重复次数的第一个条目的算法是错误的,因为第一个 if block 允许后续条目作为正确结果输出(不是第一个,而是最后一个条目实际上输出了相同的重复)
  • 如果 mostRepeatLetters为空,则返回
  • -1

关于javascript - Coderbyte 上的字母计数 I JavaScript 挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34130331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com