这是一个正在进行的学校项目,我想改进它。重点是使代码尽可能高效(或简短)。在比较计算机的选择与用户的选择时,我想通过找到所有其他 ifs 的替代方案来减少它。
代码如下:
let weapons = ["Rock", "Paper", "Scissors"];
let random = Math.floor(Math.random()*3);
let chosenOne = weapons[random];
let rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?"
+ '\n' + "If you do, enter number 1." + '\n' + "If you don't, enter number
2.");
if (rps === "1") {
alert("Remember:" + '\n' + " - Rock beats the scissors" + '\n' + " -
Paper beats the rock" + '\n' + " - The scissors cut the paper");
let weapon = prompt("Make your choice:" + '\n' + "Rock, Paper, Scissors");
weapon = weapon.charAt(0).toUpperCase() + weapon.slice(1).toLowerCase();
alert("You chose: " + weapon + '\n' + "The computer chose: " +
chosenOne);
if (weapon === chosenOne) {
alert("It's a tie! Try again to win!");
} else if (weapon === "Rock" && chosenOne === "Paper") {
alert("You lost! Paper beats the rock.");
} else if (weapon === "Paper" && chosenOne === "Scissors") {
alert("You lost! The scissors cut the paper.");
} else if (weapon === "Scissors" && chosenOne === "Rock") {
alert("You lost! The rock beats the scissors.");
} else if (weapon === "Scissors" && chosenOne === "Paper") {
alert("You won! Scissors cut the paper.");
} else if (weapon === "Paper" && chosenOne === "Rock") {
alert("You won! Paper beats the rock.");
} else if (weapon === "Rock" && chosenOne === "Scissors") {
alert("You won! The rock beats the scissors.");
}
} else if (rps === "2") {
alert("Thanks for visiting! See you later.");
} else if (rps !== "1" || rps !== "2") {
alert("Invalid option. Closing game.");
}
我考虑过使用 switch 语句,但由于我们还是初学者,我还没有完全掌握这个主题。感谢您的帮助。
最佳答案
您可以定义一个对象来定义您的移动相对于另一个移动是弱还是强。示例:
const myChoice = 'Rock'
const enemyChoice = 'Scissors'
const weapons = {
Rock: {weakTo: 'Paper', strongTo: 'Scissors'},
Paper: {weakTo: 'Scissors', strongTo: 'Rock'},
Scissors: {weakTo: 'Rock', strongTo: 'Paper'}
}
if (weapons[myChoice].strongTo === enemyChoice) {
// I won
return;
}
if (weapons[myChoice].weakTo === enemyChoice) {
// I Lost
return;
}
// tie
关于javascript - 石头剪刀布更有效的选择比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53730900/
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
是否可以在PyYAML或Ruby的Psych引擎中禁用创建anchor和引用(并有效地显式列出冗余数据)?也许我在网上搜索时遗漏了一些东西,但在Psych中似乎没有太多可用的选项,而且我也无法确定PyYAML是否允许这样做.基本原理是我必须序列化一些数据并将其以可读的形式传递给一个不是真正的技术同事进行手动验证。有些数据是多余的,但我需要以最明确的方式列出它们以提高可读性(anchor和引用是提高效率的好概念,但不是人类可读性)。Ruby和Python是我选择的工具,但如果有其他一些相当简单的方法来“展开”YAML文档,它可能就可以了。 最佳答案
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
方法应返回-1,0或1分别表示“小于”、“等于”和“大于”。对于某些类型的可排序对象,通常将排序顺序基于多个属性。以下是可行的,但我认为它看起来很笨拙:classLeagueStatsattr_accessor:points,:goal_diffdefinitializepts,gd@points=pts@goal_diff=gdenddefothercompare_pts=pointsother.pointsreturncompare_ptsunlesscompare_pts==0goal_diffother.goal_diffendend尝试一下:[LeagueStats.new(
我有两个文本文件,master.txt和926.txt。如果926.txt中有一行不在master.txt中,我想写入一个新文件notinbook.txt。我写了我能想到的最好的东西,但考虑到我是一个糟糕的/新手程序员,它失败了。这是我的东西g=File.new("notinbook.txt","w")File.open("926.txt","r")do|f|while(line=f.gets)x=line.chompifFile.open("master.txt","w")do|h|endwhile(line=h.gets)ifline.chomp!=xputslineendende
好的,所以我有了我正在使用的应用程序的这种方法,它可以在生产中使用。我的问题为什么这行得通?这是新的Ruby语法吗?defeditload_elements(current_user)unlesscurrent_user.role?(:admin)respond_todo|format|format.json{render:json=>@user}format.xml{render:xml=>@user}format.htmlendrescueActiveRecord::RecordNotFoundrespond_to_not_found(:json,:xml,:html)end
我正在尝试对某些帖子的评论使用简单的身份验证。用户使用即时ID和密码输入评论我使用“bcrypt”gem将密码存储在数据库中。在comments_controller.rb中像这样@comment=Comment.new(comment_params)bcrypted_pwd=BCrypt::Password.create(@comment.user_pwd)@comment.user_pwd=bcrypted_pwd当用户想要删除他们的评论时,我使用data-confirm-modalgem来确认数据在这部分,我必须解密用户输入的密码以与数据库中的加密密码进行比较我怎样才能解密密码,
我正在运行这样的代码:ifvalid_from>Date.today当我运行它时,我得到一个错误提示comparisonofDatewithnilfailed我假设它正在发生,因为在某些情况下valid_from是nil。有没有办法避免出现此错误? 最佳答案 你可以这样做:ifvalid_fromandvalid_from>Date.today...end这将在第一个子句上短路,因为valid_from为nil,因此为false。 关于ruby-Date与nil的比较失败-ruby,我们
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan