目前正在上课的新开发人员,正在使用Javascript&Jquery进行琐事游戏项目。我有一个漫长的条件,想看看是否可以放入for循环来压缩代码。还是有其他缩短方法?我总共有8个关于游戏的问题。
if (question1 == "Red Hot Chili Peppers") {
userCorrect ++;
}else {
userIncorrect ++;
}
if (question2 == "Rage Against The Machine") {
userCorrect ++;
}else {
userIncorrect ++;
}
if (question3 == "Nirvana") {
userCorrect ++;
}else {
userIncorrect ++;
}
if (question4 == "Sublime") {
userCorrect ++;
}else {
userIncorrect ++;
}
if (question5 == "The Black Keys") {
userCorrect ++;
}else {
userIncorrect ++;
}
if (question6 == "Dave Grohl") {
userCorrect ++;
}else {
userIncorrect ++;
}
if (question7 == "Pearl Jam") {
userCorrect ++;
}else {
userIncorrect ++;
}
if (question8 == "Big Gigantic") {
userCorrect ++;
}else {
userIncorrect ++;
}
}
游戏有效;但是,如果可能的话,希望将其压缩下来。
答案 0 :(得分:1)
改为使用一系列正确的答案(和一系列问题):
const correctAnswers = [
"Red Hot Chili Peppers",
"Rage Against The Machine",
"Nirvana"
// ...
];
// ...
// have userAnswers be an array of answers
const userCorrect = correctAnswers
.filter((correctAnswer, i) => userAnswers[i] === correctAnswer)
.length;
const userIncorrect = correctAnswers.length - userCorrect;
如果您熟悉reduce
,则可以不必在最后进行.length
检查,reduce
比较适合将数组转换为单个表达式:
const userCorrect = correctAnswers
.reduce((a, correctAnswer, i) => a + userAnswers[i] === correctAnswer, 0)
答案 1 :(得分:0)
将正确答案存储在一个数组中,将用户的猜测存储在另一个数组中。然后,遍历正确的答案,并根据用户的guess数组中的相应答案进行检查。
这可以通过几种不同的方式实现,但是由于您只是在学习,因此我将其保持得非常简单。它依赖于第一个用.forEach()
循环的数组,然后使用ternary operator来决定是否应提高分数。
client.on("guildMemberAdd", member => {
let mlogchannel = member.guild.channels.find((channel => channel.name === "member-logging"));
if (mlogchannel) {
console.log(client.users.find(user => user.id === member.id).createdAt())
var cdate = moment.utc(User.createdAt()).format("dddd, MMMM Do YYYY, HH:mm");
const sInfo = new Discord.RichEmbed()
.setTitle(`Member joined`)
.setAuthor(`${member.displayName}`)
.setColor(8528115)
.setFooter(`User ID: ${member.id}`)
.setTimestamp()
.setThumbnail(member.user.createdAt())
.addField("Total members", `${Guild.members.filter(member => !member.user.bot).size}`, true)
.addField("Creation Date:", `${cdate}`, true);
let ageS = moment(cdate).fromNow(true)
let ageA = ageS.split(" ");
if (ageA[1] = "days" && ageA[2] >= 30) {
Guild.channels.find((channel => channel.name === "member-logging").send(sInfo));
mlogchannel.send("**WARNING!**\nThis account is less than 30 days old and may have been made to bypass a server mute or ban!")
}
if (ageA[1] != "days") {
mlogchannel.send(sInfo)
}
if (!mlogchannel) {
return console.log(`${Guild.name}:${Guild.ID} Has not set up a member log channel!`)
}
}
})
答案 2 :(得分:0)
如果将正确的answers
保存在一个数组上,并将用户responses
保存在另一个数组索引与答案匹配的答案上,则可以使用循环比较它们并计算正确的答案。 / p>
const answers = [
"Red Hot Chili Peppers",
"Rage Against The Machine",
"Nirvana",
"Sublime",
"The Black Keys",
"Dave Grohl",
"Pearl Jam",
"Big Gigantic"
];
let userResponses = [
"Red Hot Chili Peppers",
"Pink Floyd",
"Nirvana",
"Sublime",
"The Black Keys",
"Bob Marley",
"Pearl Jam",
"Artic Monkeys"
];
let goods = 0;
userResponses.forEach((x, idx) =>
{
goods += (x === answers[idx]);
});
console.log("Corrects: " + goods);
console.log("Incorrects: " + (answers.length - goods));
答案 3 :(得分:0)
首先创建正确答案的数组。然后,将所有用户的答案存储在不同的数组中。将correctCount
计数器初始化为0,然后使用简单的for loop
遍历列表,将每个值相互比较。另外,由于incorectCount
易于计算,因此您不必单独进行计数。
const correctAnswers = [
"Red Hot Chili Peppers",
"Rage Against The Machine",
"Nirvana",
"Sublime"
];
const userAnswers = [
"Red Hot Chili Peppers",
"Rage Against The Machine",
"Nirvana",
"Metallica"
];
let correctCount = 0;
for (let i = 0; i < correctAnswers.length; i++) {
if (correctAnswers[i] === userAnswers[i]) {
correctCount++;
}
}
console.log("Correct answers:" + correctCount);
console.log("Incorrect answers:" + (correctAnswers.length - correctCount));