CodeCademy:JavaScript构建“Rock,Paper,Scissors”:帮助调试自己的代码 [英] CodeCademy: JavaScript build"Rock, Paper, Scissors": Help with Debugging own code

查看:50
本文介绍了CodeCademy:JavaScript构建“Rock,Paper,Scissors”:帮助调试自己的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新程序员并找到了这个网站。这将是我在这个网站上的第一个问题,所以如果这个问题不是最好,我很抱歉。我正在一个名为codecademy的网站上学习Javascript。当我正在进行一个关于用javascript制作石头剪刀的项目时,我遇到了一个问题。

如果你想找到codecademy问的问题,那就去搜索一下吧。无论如何,如果可以的话,我希望你帮我调试我的代码,因为我对编程很新,并希望得到一些帮助,因为我不明白我哪里出错了。请记住,我是新人,如果你走得太深,我将无法理解你的答案,如果可以的话,让我简单。感谢您的时间,抱歉我的文字墙!

--------------------------- -------------------------------------------------- -------

这是我的代码:



  var  userChoice = prompt( 你选择摇滚,纸张还是剪刀? ); 
var computerChoice = Math.random();
if (computerChoice < 0 34 ){
computerChoice = 摇滚;
} 其他 如果(computerChoice < = 0 67 ){
computerChoice = paper;
} else {
computerChoice = 剪刀;
} console.log( 计算机: + computerChoice);

var compare = function(choice1,choice2){

if (choice1 === choice2){
return 结果是平局!;
}

其他 如果(choice1 === rock){
if (choice2 === 剪刀){
return rock wins;
}
其他 {
return paper wins;
}
}

其他 如果(choice1 === paper){
if (choice2 === rock){
return paper wins;
}
}
其他 {
返回 < span class =code-string>
剪刀获胜;
}
else if (choice1 === 剪刀){
if (choice2 == = rock){
return rock wins;
}
其他 {
return 剪刀获胜;
}
}

}



---------------- -------------------------------------------------- ------------------

当我运行我的代码时,控制台会以意外的令牌响应。此外,如果您再看到问题,请解决这些问题。非常感谢你的时间,如果你能弄清楚我哪里出错了,我将不胜感激!



快乐编码每个人!

解决方案

回答你的问题:帮助自己调试代码,使用调试器。你将解决两个问题,而不是一个:调试你的代码并学习如何调试。



现在,请看我对这个问题的评论。另外,我会这样说:使用字符串来表示某个状态也很糟糕。这篇非常简单实用的文章将为您提供更好的主意:

http:/ /stravid.com/en/cleaner-javascript-code-with-enums [ ^ ]。



根据这个想法,你可以用这个对象来表示每个动作的状态:

  var  Moves = {
Rock: 0
纸张: 1
剪刀: 2
};

并按照文章中的说明使用它。游戏的想法是使用三者之间的非传递关系。明确定义:定义一个谓词函数对象,它接受两个 Moves 值并返回布尔值(哪一个胜出)并在游戏中使用它。



要好好使用它,最好不要使用提示符。相反,你可以写3个按钮,摇滚,纸和剪刀。每个按钮上的Click事件处理程序将调用某个函数,参数等于 Moves.Rock Moves.Paper 等。



我想我向你解释了所有重要的要点。剩下的部分是Javascript和编程基础知识,以及一些调试技巧。改变是,如果你以一种文化的方式再次编写这个代码,它可能需要很少甚至不需要调试。







另外,使用 console.log 对游戏来说几乎没用或者不可接受;它将在特殊日志窗口中显示文本,而不是在网页中显示。您可以使用一些HTML元素(例如,使用 document.getElementById 查找)并添加子元素(文本节点或其他任何内容)。



[结束编辑]



祝你好运。新年快乐!



-SA


除了SA的答案 [ ^ ],你有错误的if else(..)else (..)if else(..)代码

check

  else   if (choice1 ===   paper){
if (choice2 === rock){
return 纸赢;
}
}
其他 {
返回 < span class =code-string> 剪刀获胜;
} 其他 如果(...)





应该是

 其他  if (choice1 ===   paper){ 
if (choice2 === rock ){
return 纸赢;
}
其他 {
return 剪刀获胜;
}
} 其他 如果(...)



示例代码:(只修复代码中的问题,最好使用SA建议的枚举。)

  var  userChoice = prompt( 您选择摇滚,纸张还是剪刀? ); 
var computerChoice = Math .random();
if (computerChoice< 0 34 ){
computerChoice = rock;
} 其他 如果(computerChoice< = 0 67 ){
computerChoice = ;
} else {
computerChoice = 剪刀;
} console .log( 计算机: + computerChoice);
var compare = function (choice1,choice2){
if (choice1 === choice2){
return 结果是平局!;
} 其他 如果(choice1 === rock
{
if (choice2 == = 剪刀
{
return rock wins;
} else
{
return paper wins;
}
} 其他 如果(choice1 === paper
{
if (choice2 === rock
{
return paper wins;
} else
{
return 剪刀获胜;
}
} 其他 如果(choice1 === 剪刀
{
if (choice2 === rock
{
return rock wins;
} else
{
return 剪刀获胜;
}
} 其他
{
返回 < span class =code-string>
输入错误;
}
};
alert(compare(userChoice,computerChoice));


我发现的问题是,在你有return命令之后你需要括号括起来你希望返回所以看起来像这样:返回(返回什么)

你还有一些不必要的东西



希望这个帮助!

I am a new programmer and found this site. This will be my first question on this site so I am sorry if this question is not the best. I am learning Javascript on a website called codecademy. As I was going through a project about making rock paper scissors with javascript, I came upon a problem.
If you would like to find the questions that codecademy asks, just search it up. Anyways, if you could, I would like you to help me debug my code because I am quite new to programming and would like some help because I don't understand where I went wrong. Please remember that I am new and I will not be able to understand your answers if you go too deep, make it "simple" for me if you could. Thank you for your time, and sorry for my huge wall of text!
------------------------------------------------------------------------------------
Here is the code I have:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {

    if(choice1 === choice2) {
        return "The result is a tie!";
    }

    else if(choice1 === "rock") {
        if(choice2 === "scissors") {
            return "rock wins";
        }
    else{
        return"paper wins";
    }
    }

    else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }
    }
    else {
        return "scissors wins";
    }
    else if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return "rock wins";
        }
    else {
        return "scissors wins";
    }
    }

}


------------------------------------------------------------------------------------
When I run my code, the console responds with unexpected token else. Also, if you see anymore problems, please put down a solution to those problems too. Thank you so much for your time and I would be grateful if you can figure out where I went wrong!

Happy coding everyone!

解决方案

To answer your question: to help yourself to debug the code, use the debugger. You will solve two problems instead of one: debug your code and learn how to debug.

Now, please see my comment to the question. In addition, I'll say this: it's also pretty bad to use strings to represent some state. This very simple and useful article will give you much better idea:
http://stravid.com/en/cleaner-javascript-code-with-enums[^].

According to this idea, you could have this object to represent the state of each move:

var Moves = {
    Rock: 0,
    Paper: 1,
    Scissors: 2
};

and use it as explained in the article. The idea of the game is to use the non-transitive relationship between the three. Define it explicitly: define a predicate function objects which accept two Moves values and returns Boolean (which one wins) and use it in your game.

To use it well, it would be much better not to use the prompt. Instead, you could write 3 buttons, "Rock", "Paper" and "Scissors". Click event handler on each button would call some function with the parameter equal to Moves.Rock, Moves.Paper, etc.

I think I explained all important key points to you. Remaining part is knowledge of Javascript and programming basics, and some debugging skills. Changes are, if you write this code again in a cultured way, it may require very little to no debugging.

[EDIT]

Also, using console.log is hardly useful or acceptable for a game; it will show text on a special log window, not in a Web page. You could use some HTML element (find it, say, using document.getElementById) and add child element (text nodes or whatever else) to it.

[END EDIT]

Good luck. Happy New Year!

—SA


In addition to SA's Answer[^], you have incorrect if else(..) else (..) if else (..) code
check

else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }
    }
    else {
        return "scissors wins";
    }else if(...)



it should be

else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }
    else {
        return "scissors wins";
    }
} else if(...)


sample code: (only fix the issue in your code, you better use enums as SA suggested.)

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    }else if(choice1 ==="rock")
    {
        if(choice2 === "scissors")
        {
            return "rock wins";
        }else
        {
            return "paper wins";
        }
    }else if(choice1 ==="paper")
    {
        if(choice2 === "rock")
        {
            return "paper wins";
        }else
        {
            return "scissors wins";
        }
    }else if(choice1 ==="scissors")
    {
        if(choice2 === "rock")
        {
            return "rock wins";
        }else
        {
            return "scissors wins";
        }
    }else
    {
         return "incorrect input";
    }
};
alert(compare(userChoice,computerChoice));


The problem I found is that after where you have the return command you need parenthesis around what you wish to return so it would look like this: return (What is being returned)
Also you have some unnecessary else's

Hope this helps!


这篇关于CodeCademy:JavaScript构建“Rock,Paper,Scissors”:帮助调试自己的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆