首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“牛与牛”游戏

“牛与牛”游戏
EN

Code Review用户
提问于 2016-01-29 22:39:50
回答 1查看 756关注 0票数 -7

我们正在进行一项任务,在这个任务中,我们必须生成代码,在游戏中扮演一个玩家,牛和公牛。我正在处理一个基于codeSizecodeChars值生成代码的函数。CodeSize是代码的大小,CodeChars是alpha还是数字,这取决于用户所选择的内容。然而,我似乎无法骑上这些复制品。

代码语言:javascript
复制
// Check if we are not allowed to include duplicate characters:
             if (nodupes === true) {
                 // True if character already used:
                 var added = (used.indexOf(chr) !== -1);
                 // Skip the Character if already used:
                 if (added === true) {
                     continue;
                    }
                 // Add Character to the list of used ones:
                 used += chr;
//                   alert(used);
                }
             // Add Character to the output string:
             output += chr;
            //    alert(output);
            } 
 while (output.length < codeSize);
      //   alert(typeof(output));
        //     alert(output);
             return output;  // Not working Correctly string still has duplicate characters.
         if (output.length == codeSize) {
             result = output;
         alert(result);
            }
        }
EN

回答 1

Code Review用户

发布于 2016-03-03 16:09:26

首先,让我们清理注释和缩进,因为这里的内容是不可读的。

首先修复压痕..。

代码语言:javascript
复制
        // Check if we are not allowed to include duplicate characters:
        if (nodupes === true) {
            // True if character already used:
            var added = (used.indexOf(chr) !== -1);
            // Skip the Character if already used:
            if (added === true) {
                continue;
            }
            // Add Character to the list of used ones:
            used += chr;
            //                   alert(used);
        }
        // Add Character to the output string:
        output += chr;
        //    alert(output);
    } 
    while (output.length < codeSize);
    //   alert(typeof(output));
    //     alert(output);
    return output;  // Not working Correctly string still has duplicate characters.
    if (output.length == codeSize) {
        result = output;
        alert(result);
    }
}

这完全没有意义。

它以前没有,但现在我们至少可以看到它有什么问题:

  1. 你的代码半途而废。大概是在一个并发循环中。
  2. 返回输出,然后函数继续使用更多代码。

这些是主要的问题。

然而,第二个问题是致命的;这是错误的代码。特别是对于注释"//不能正确工作“,字符串仍然有重复字符。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/118321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档