Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解读编码红细胞中字母计数(str)的答案_Javascript - Fatal编程技术网

Javascript 解读编码红细胞中字母计数(str)的答案

Javascript 解读编码红细胞中字母计数(str)的答案,javascript,Javascript,非常愚蠢,希望这个问题不是愚蠢到无可救药 我一直在努力解决Coderbyte上的Javascript问题,并无可救药地坚持下面的lettercount练习: 已在线找到以下解决方案(有效): 函数LetterCountI(str){ 变量x=str.split(“”) var计数=0; 对于(var i=0;i这不能放在if中,因为if在for中。这是构造这种算法的一种非常典型的方法。它基本上通过for循环来查找结果,如果找不到,则返回默认值。另一种构造它的方法是只能有一个return语句(效

非常愚蠢,希望这个问题不是愚蠢到无可救药

我一直在努力解决Coderbyte上的Javascript问题,并无可救药地坚持下面的lettercount练习:

已在线找到以下解决方案(有效):

函数LetterCountI(str){
变量x=str.split(“”)
var计数=0;

对于(var i=0;i这不能放在
if
中,因为
if
for
中。这是构造这种算法的一种非常典型的方法。它基本上通过
for
循环来查找结果,如果找不到,则返回默认值。另一种构造它的方法是只能有一个
return
语句(效率稍低):

函数LetterCountI(str){
var x=str.split(“”);
var计数=0;
var-res=-1;

对于(var i=0;iThanks neelsg),这有助于澄清它!
 function LetterCountI(str){

   var x=str.split(" ") 
   var count=0; 
   for(var i=0; i<x.length; i++)//getting the word
     var word=x[i]
     for(var j=0; j<word.length; j++)//getting the letter
      var letter=word[j]
       for(var k=0; k<word.length; k++)//comparing word
         if(j !== k) { /* verify that the letter being pased is not literally the same as in the higher for loop */
           if(letter===word[k]){
           count=count+1; 
              if(count>1){
               return word;  
           }
        }
     }   
  return -1;
}
function LetterCountI(str){

   var x = str.split(" ");
   var count = 0;
   var res = -1;
   for(var i=0; i<x.length; i++) {//getting the word
     var word=x[i];
     for(var j=0; j<word.length; j++) {//getting the letter
       var letter=word[j];
       for(var k=0; k<word.length; k++) {//comparing word
         if(j !== k) { /* verify that the letter being passed is not literally the same as in the higher for loop */
           if(letter === word[k] && res === -1) {
              count=count+1; 
              if(count>1){
                 res = word;
              }
           }
         }
       }
     }
   }
   return res;
}